]>
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 <[email protected]> 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> |
8581fd40 | 18 | #include <linux/module.h> |
cd82cca7 CH |
19 | #include "blk.h" |
20 | ||
fac7c6d5 | 21 | static inline struct inode *bdev_file_inode(struct file *file) |
cd82cca7 CH |
22 | { |
23 | return file->f_mapping->host; | |
24 | } | |
25 | ||
26 | static int blkdev_get_block(struct inode *inode, sector_t iblock, | |
27 | struct buffer_head *bh, int create) | |
28 | { | |
29 | bh->b_bdev = I_BDEV(inode); | |
30 | bh->b_blocknr = iblock; | |
31 | set_buffer_mapped(bh); | |
32 | return 0; | |
33 | } | |
34 | ||
16458cf3 | 35 | static blk_opf_t dio_bio_write_op(struct kiocb *iocb) |
cd82cca7 | 36 | { |
16458cf3 | 37 | blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE; |
cd82cca7 CH |
38 | |
39 | /* avoid the need for a I/O completion work item */ | |
91b94c5d | 40 | if (iocb_is_dsync(iocb)) |
16458cf3 BVA |
41 | opf |= REQ_FUA; |
42 | return opf; | |
cd82cca7 CH |
43 | } |
44 | ||
37fee2e4 KB |
45 | static bool blkdev_dio_unaligned(struct block_device *bdev, loff_t pos, |
46 | struct iov_iter *iter) | |
47 | { | |
b1a000d3 KB |
48 | return pos & (bdev_logical_block_size(bdev) - 1) || |
49 | !bdev_iter_is_aligned(bdev, iter); | |
37fee2e4 KB |
50 | } |
51 | ||
cd82cca7 CH |
52 | #define DIO_INLINE_BIO_VECS 4 |
53 | ||
cd82cca7 CH |
54 | static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb, |
55 | struct iov_iter *iter, unsigned int nr_pages) | |
56 | { | |
4e762d86 | 57 | struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); |
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 | 63 | |
37fee2e4 | 64 | if (blkdev_dio_unaligned(bdev, pos, iter)) |
cd82cca7 CH |
65 | return -EINVAL; |
66 | ||
67 | if (nr_pages <= DIO_INLINE_BIO_VECS) | |
68 | vecs = inline_vecs; | |
69 | else { | |
70 | vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec), | |
71 | GFP_KERNEL); | |
72 | if (!vecs) | |
73 | return -ENOMEM; | |
74 | } | |
75 | ||
49add496 CH |
76 | if (iov_iter_rw(iter) == READ) { |
77 | bio_init(&bio, bdev, vecs, nr_pages, REQ_OP_READ); | |
fcb14cb1 | 78 | if (user_backed_iter(iter)) |
49add496 CH |
79 | should_dirty = true; |
80 | } else { | |
81 | bio_init(&bio, bdev, vecs, nr_pages, dio_bio_write_op(iocb)); | |
82 | } | |
6549a874 | 83 | bio.bi_iter.bi_sector = pos >> SECTOR_SHIFT; |
cd82cca7 CH |
84 | bio.bi_ioprio = iocb->ki_ioprio; |
85 | ||
86 | ret = bio_iov_iter_get_pages(&bio, iter); | |
87 | if (unlikely(ret)) | |
88 | goto out; | |
89 | ret = bio.bi_iter.bi_size; | |
90 | ||
49add496 | 91 | if (iov_iter_rw(iter) == WRITE) |
cd82cca7 | 92 | task_io_account_write(ret); |
49add496 | 93 | |
cd82cca7 CH |
94 | if (iocb->ki_flags & IOCB_NOWAIT) |
95 | bio.bi_opf |= REQ_NOWAIT; | |
9650b453 ML |
96 | |
97 | submit_bio_wait(&bio); | |
cd82cca7 CH |
98 | |
99 | bio_release_pages(&bio, should_dirty); | |
100 | if (unlikely(bio.bi_status)) | |
101 | ret = blk_status_to_errno(bio.bi_status); | |
102 | ||
103 | out: | |
104 | if (vecs != inline_vecs) | |
105 | kfree(vecs); | |
106 | ||
107 | bio_uninit(&bio); | |
108 | ||
109 | return ret; | |
110 | } | |
111 | ||
09ce8744 | 112 | enum { |
e71aa913 PB |
113 | DIO_SHOULD_DIRTY = 1, |
114 | DIO_IS_SYNC = 2, | |
09ce8744 JA |
115 | }; |
116 | ||
cd82cca7 CH |
117 | struct blkdev_dio { |
118 | union { | |
119 | struct kiocb *iocb; | |
120 | struct task_struct *waiter; | |
121 | }; | |
122 | size_t size; | |
123 | atomic_t ref; | |
09ce8744 | 124 | unsigned int flags; |
6155631a | 125 | struct bio bio ____cacheline_aligned_in_smp; |
cd82cca7 CH |
126 | }; |
127 | ||
128 | static struct bio_set blkdev_dio_pool; | |
129 | ||
cd82cca7 CH |
130 | static void blkdev_bio_end_io(struct bio *bio) |
131 | { | |
132 | struct blkdev_dio *dio = bio->bi_private; | |
09ce8744 | 133 | bool should_dirty = dio->flags & DIO_SHOULD_DIRTY; |
cd82cca7 CH |
134 | |
135 | if (bio->bi_status && !dio->bio.bi_status) | |
136 | dio->bio.bi_status = bio->bi_status; | |
137 | ||
e71aa913 | 138 | if (atomic_dec_and_test(&dio->ref)) { |
09ce8744 | 139 | if (!(dio->flags & DIO_IS_SYNC)) { |
cd82cca7 CH |
140 | struct kiocb *iocb = dio->iocb; |
141 | ssize_t ret; | |
142 | ||
3e08773c CH |
143 | WRITE_ONCE(iocb->private, NULL); |
144 | ||
cd82cca7 CH |
145 | if (likely(!dio->bio.bi_status)) { |
146 | ret = dio->size; | |
147 | iocb->ki_pos += ret; | |
148 | } else { | |
149 | ret = blk_status_to_errno(dio->bio.bi_status); | |
150 | } | |
151 | ||
6b19b766 | 152 | dio->iocb->ki_complete(iocb, ret); |
e71aa913 | 153 | bio_put(&dio->bio); |
cd82cca7 CH |
154 | } else { |
155 | struct task_struct *waiter = dio->waiter; | |
156 | ||
157 | WRITE_ONCE(dio->waiter, NULL); | |
158 | blk_wake_io_task(waiter); | |
159 | } | |
160 | } | |
161 | ||
162 | if (should_dirty) { | |
163 | bio_check_pages_dirty(bio); | |
164 | } else { | |
165 | bio_release_pages(bio, false); | |
166 | bio_put(bio); | |
167 | } | |
168 | } | |
169 | ||
170 | static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, | |
171 | unsigned int nr_pages) | |
172 | { | |
4e762d86 | 173 | struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); |
cd82cca7 CH |
174 | struct blk_plug plug; |
175 | struct blkdev_dio *dio; | |
176 | struct bio *bio; | |
cd82cca7 | 177 | bool is_read = (iov_iter_rw(iter) == READ), is_sync; |
16458cf3 | 178 | blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb); |
cd82cca7 | 179 | loff_t pos = iocb->ki_pos; |
cd82cca7 CH |
180 | int ret = 0; |
181 | ||
37fee2e4 | 182 | if (blkdev_dio_unaligned(bdev, pos, iter)) |
cd82cca7 CH |
183 | return -EINVAL; |
184 | ||
0df71650 MS |
185 | if (iocb->ki_flags & IOCB_ALLOC_CACHE) |
186 | opf |= REQ_ALLOC_CACHE; | |
187 | bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL, | |
188 | &blkdev_dio_pool); | |
cd82cca7 | 189 | dio = container_of(bio, struct blkdev_dio, bio); |
e71aa913 PB |
190 | atomic_set(&dio->ref, 1); |
191 | /* | |
192 | * Grab an extra reference to ensure the dio structure which is embedded | |
193 | * into the first bio stays around. | |
194 | */ | |
195 | bio_get(bio); | |
196 | ||
09ce8744 JA |
197 | is_sync = is_sync_kiocb(iocb); |
198 | if (is_sync) { | |
199 | dio->flags = DIO_IS_SYNC; | |
cd82cca7 | 200 | dio->waiter = current; |
cd82cca7 | 201 | } else { |
09ce8744 | 202 | dio->flags = 0; |
cd82cca7 CH |
203 | dio->iocb = iocb; |
204 | } | |
205 | ||
206 | dio->size = 0; | |
fcb14cb1 | 207 | if (is_read && user_backed_iter(iter)) |
09ce8744 | 208 | dio->flags |= DIO_SHOULD_DIRTY; |
cd82cca7 | 209 | |
25d207dc | 210 | blk_start_plug(&plug); |
cd82cca7 CH |
211 | |
212 | for (;;) { | |
6549a874 | 213 | bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT; |
cd82cca7 CH |
214 | bio->bi_private = dio; |
215 | bio->bi_end_io = blkdev_bio_end_io; | |
216 | bio->bi_ioprio = iocb->ki_ioprio; | |
217 | ||
218 | ret = bio_iov_iter_get_pages(bio, iter); | |
219 | if (unlikely(ret)) { | |
220 | bio->bi_status = BLK_STS_IOERR; | |
221 | bio_endio(bio); | |
222 | break; | |
223 | } | |
67d59247 JA |
224 | if (iocb->ki_flags & IOCB_NOWAIT) { |
225 | /* | |
226 | * This is nonblocking IO, and we need to allocate | |
227 | * another bio if we have data left to map. As we | |
228 | * cannot guarantee that one of the sub bios will not | |
229 | * fail getting issued FOR NOWAIT and as error results | |
230 | * are coalesced across all of them, be safe and ask for | |
231 | * a retry of this from blocking context. | |
232 | */ | |
233 | if (unlikely(iov_iter_count(iter))) { | |
234 | bio_release_pages(bio, false); | |
235 | bio_clear_flag(bio, BIO_REFFED); | |
236 | bio_put(bio); | |
237 | blk_finish_plug(&plug); | |
238 | return -EAGAIN; | |
239 | } | |
240 | bio->bi_opf |= REQ_NOWAIT; | |
241 | } | |
cd82cca7 CH |
242 | |
243 | if (is_read) { | |
09ce8744 | 244 | if (dio->flags & DIO_SHOULD_DIRTY) |
cd82cca7 CH |
245 | bio_set_pages_dirty(bio); |
246 | } else { | |
cd82cca7 CH |
247 | task_io_account_write(bio->bi_iter.bi_size); |
248 | } | |
cd82cca7 CH |
249 | dio->size += bio->bi_iter.bi_size; |
250 | pos += bio->bi_iter.bi_size; | |
251 | ||
252 | nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS); | |
253 | if (!nr_pages) { | |
3e08773c | 254 | submit_bio(bio); |
cd82cca7 CH |
255 | break; |
256 | } | |
e71aa913 | 257 | atomic_inc(&dio->ref); |
cd82cca7 | 258 | submit_bio(bio); |
07888c66 | 259 | bio = bio_alloc(bdev, nr_pages, opf, GFP_KERNEL); |
cd82cca7 CH |
260 | } |
261 | ||
25d207dc | 262 | blk_finish_plug(&plug); |
cd82cca7 CH |
263 | |
264 | if (!is_sync) | |
265 | return -EIOCBQUEUED; | |
266 | ||
267 | for (;;) { | |
268 | set_current_state(TASK_UNINTERRUPTIBLE); | |
269 | if (!READ_ONCE(dio->waiter)) | |
270 | break; | |
25d207dc | 271 | blk_io_schedule(); |
cd82cca7 CH |
272 | } |
273 | __set_current_state(TASK_RUNNING); | |
274 | ||
275 | if (!ret) | |
276 | ret = blk_status_to_errno(dio->bio.bi_status); | |
277 | if (likely(!ret)) | |
278 | ret = dio->size; | |
279 | ||
280 | bio_put(&dio->bio); | |
281 | return ret; | |
282 | } | |
283 | ||
54a88eb8 PB |
284 | static void blkdev_bio_end_io_async(struct bio *bio) |
285 | { | |
286 | struct blkdev_dio *dio = container_of(bio, struct blkdev_dio, bio); | |
287 | struct kiocb *iocb = dio->iocb; | |
288 | ssize_t ret; | |
289 | ||
bb49c6fa SG |
290 | WRITE_ONCE(iocb->private, NULL); |
291 | ||
54a88eb8 PB |
292 | if (likely(!bio->bi_status)) { |
293 | ret = dio->size; | |
294 | iocb->ki_pos += ret; | |
295 | } else { | |
296 | ret = blk_status_to_errno(bio->bi_status); | |
297 | } | |
298 | ||
b6773cdb | 299 | iocb->ki_complete(iocb, ret); |
54a88eb8 PB |
300 | |
301 | if (dio->flags & DIO_SHOULD_DIRTY) { | |
302 | bio_check_pages_dirty(bio); | |
303 | } else { | |
304 | bio_release_pages(bio, false); | |
305 | bio_put(bio); | |
306 | } | |
307 | } | |
308 | ||
309 | static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb, | |
310 | struct iov_iter *iter, | |
311 | unsigned int nr_pages) | |
312 | { | |
4e762d86 | 313 | struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); |
b77c88c2 | 314 | bool is_read = iov_iter_rw(iter) == READ; |
16458cf3 | 315 | blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb); |
54a88eb8 PB |
316 | struct blkdev_dio *dio; |
317 | struct bio *bio; | |
318 | loff_t pos = iocb->ki_pos; | |
319 | int ret = 0; | |
320 | ||
37fee2e4 | 321 | if (blkdev_dio_unaligned(bdev, pos, iter)) |
54a88eb8 PB |
322 | return -EINVAL; |
323 | ||
0df71650 MS |
324 | if (iocb->ki_flags & IOCB_ALLOC_CACHE) |
325 | opf |= REQ_ALLOC_CACHE; | |
326 | bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL, | |
327 | &blkdev_dio_pool); | |
54a88eb8 PB |
328 | dio = container_of(bio, struct blkdev_dio, bio); |
329 | dio->flags = 0; | |
330 | dio->iocb = iocb; | |
54a88eb8 | 331 | bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT; |
54a88eb8 PB |
332 | bio->bi_end_io = blkdev_bio_end_io_async; |
333 | bio->bi_ioprio = iocb->ki_ioprio; | |
334 | ||
1bb6b810 PB |
335 | if (iov_iter_is_bvec(iter)) { |
336 | /* | |
337 | * Users don't rely on the iterator being in any particular | |
338 | * state for async I/O returning -EIOCBQUEUED, hence we can | |
339 | * avoid expensive iov_iter_advance(). Bypass | |
340 | * bio_iov_iter_get_pages() and set the bvec directly. | |
341 | */ | |
342 | bio_iov_bvec_set(bio, iter); | |
343 | } else { | |
344 | ret = bio_iov_iter_get_pages(bio, iter); | |
345 | if (unlikely(ret)) { | |
75feae73 | 346 | bio_put(bio); |
1bb6b810 PB |
347 | return ret; |
348 | } | |
54a88eb8 PB |
349 | } |
350 | dio->size = bio->bi_iter.bi_size; | |
351 | ||
b77c88c2 | 352 | if (is_read) { |
fcb14cb1 | 353 | if (user_backed_iter(iter)) { |
54a88eb8 PB |
354 | dio->flags |= DIO_SHOULD_DIRTY; |
355 | bio_set_pages_dirty(bio); | |
356 | } | |
357 | } else { | |
54a88eb8 PB |
358 | task_io_account_write(bio->bi_iter.bi_size); |
359 | } | |
360 | ||
54a88eb8 | 361 | if (iocb->ki_flags & IOCB_HIPRI) { |
842e39b0 | 362 | bio->bi_opf |= REQ_POLLED | REQ_NOWAIT; |
54a88eb8 PB |
363 | submit_bio(bio); |
364 | WRITE_ONCE(iocb->private, bio); | |
365 | } else { | |
842e39b0 PB |
366 | if (iocb->ki_flags & IOCB_NOWAIT) |
367 | bio->bi_opf |= REQ_NOWAIT; | |
54a88eb8 PB |
368 | submit_bio(bio); |
369 | } | |
370 | return -EIOCBQUEUED; | |
371 | } | |
372 | ||
cd82cca7 CH |
373 | static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
374 | { | |
375 | unsigned int nr_pages; | |
376 | ||
377 | if (!iov_iter_count(iter)) | |
378 | return 0; | |
379 | ||
380 | nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1); | |
54a88eb8 PB |
381 | if (likely(nr_pages <= BIO_MAX_VECS)) { |
382 | if (is_sync_kiocb(iocb)) | |
383 | return __blkdev_direct_IO_simple(iocb, iter, nr_pages); | |
384 | return __blkdev_direct_IO_async(iocb, iter, nr_pages); | |
385 | } | |
cd82cca7 CH |
386 | return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages)); |
387 | } | |
388 | ||
389 | static int blkdev_writepage(struct page *page, struct writeback_control *wbc) | |
390 | { | |
391 | return block_write_full_page(page, blkdev_get_block, wbc); | |
392 | } | |
393 | ||
2c69e205 | 394 | static int blkdev_read_folio(struct file *file, struct folio *folio) |
cd82cca7 | 395 | { |
2c69e205 | 396 | return block_read_full_folio(folio, blkdev_get_block); |
cd82cca7 CH |
397 | } |
398 | ||
399 | static void blkdev_readahead(struct readahead_control *rac) | |
400 | { | |
401 | mpage_readahead(rac, blkdev_get_block); | |
402 | } | |
403 | ||
404 | static int blkdev_write_begin(struct file *file, struct address_space *mapping, | |
9d6b0cd7 | 405 | loff_t pos, unsigned len, struct page **pagep, void **fsdata) |
cd82cca7 | 406 | { |
b3992d1e | 407 | return block_write_begin(mapping, pos, len, pagep, blkdev_get_block); |
cd82cca7 CH |
408 | } |
409 | ||
410 | static int blkdev_write_end(struct file *file, struct address_space *mapping, | |
411 | loff_t pos, unsigned len, unsigned copied, struct page *page, | |
412 | void *fsdata) | |
413 | { | |
414 | int ret; | |
415 | ret = block_write_end(file, mapping, pos, len, copied, page, fsdata); | |
416 | ||
417 | unlock_page(page); | |
418 | put_page(page); | |
419 | ||
420 | return ret; | |
421 | } | |
422 | ||
cd82cca7 | 423 | const struct address_space_operations def_blk_aops = { |
e621900a | 424 | .dirty_folio = block_dirty_folio, |
7ba13abb | 425 | .invalidate_folio = block_invalidate_folio, |
2c69e205 | 426 | .read_folio = blkdev_read_folio, |
cd82cca7 CH |
427 | .readahead = blkdev_readahead, |
428 | .writepage = blkdev_writepage, | |
429 | .write_begin = blkdev_write_begin, | |
430 | .write_end = blkdev_write_end, | |
67235182 | 431 | .migrate_folio = buffer_migrate_folio_norefs, |
cd82cca7 CH |
432 | .is_dirty_writeback = buffer_check_dirty_writeback, |
433 | }; | |
434 | ||
435 | /* | |
436 | * for a block special file file_inode(file)->i_size is zero | |
437 | * so we compute the size by hand (just as in block_read/write above) | |
438 | */ | |
439 | static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence) | |
440 | { | |
441 | struct inode *bd_inode = bdev_file_inode(file); | |
442 | loff_t retval; | |
443 | ||
444 | inode_lock(bd_inode); | |
445 | retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode)); | |
446 | inode_unlock(bd_inode); | |
447 | return retval; | |
448 | } | |
449 | ||
450 | static int blkdev_fsync(struct file *filp, loff_t start, loff_t end, | |
451 | int datasync) | |
452 | { | |
4e762d86 | 453 | struct block_device *bdev = I_BDEV(filp->f_mapping->host); |
cd82cca7 CH |
454 | int error; |
455 | ||
456 | error = file_write_and_wait_range(filp, start, end); | |
457 | if (error) | |
458 | return error; | |
459 | ||
460 | /* | |
461 | * There is no need to serialise calls to blkdev_issue_flush with | |
462 | * i_mutex and doing so causes performance issues with concurrent | |
463 | * O_SYNC writers to a block device. | |
464 | */ | |
465 | error = blkdev_issue_flush(bdev); | |
466 | if (error == -EOPNOTSUPP) | |
467 | error = 0; | |
468 | ||
469 | return error; | |
470 | } | |
471 | ||
05bdb996 CH |
472 | blk_mode_t file_to_blk_mode(struct file *file) |
473 | { | |
474 | blk_mode_t mode = 0; | |
475 | ||
476 | if (file->f_mode & FMODE_READ) | |
477 | mode |= BLK_OPEN_READ; | |
478 | if (file->f_mode & FMODE_WRITE) | |
479 | mode |= BLK_OPEN_WRITE; | |
ee3249a8 | 480 | if (file->private_data) |
05bdb996 CH |
481 | mode |= BLK_OPEN_EXCL; |
482 | if (file->f_flags & O_NDELAY) | |
483 | mode |= BLK_OPEN_NDELAY; | |
484 | ||
485 | /* | |
486 | * If all bits in O_ACCMODE set (aka O_RDWR | O_WRONLY), the floppy | |
487 | * driver has historically allowed ioctls as if the file was opened for | |
488 | * writing, but does not allow and actual reads or writes. | |
489 | */ | |
490 | if ((file->f_flags & O_ACCMODE) == (O_RDWR | O_WRONLY)) | |
491 | mode |= BLK_OPEN_WRITE_IOCTL; | |
492 | ||
493 | return mode; | |
494 | } | |
495 | ||
cd82cca7 CH |
496 | static int blkdev_open(struct inode *inode, struct file *filp) |
497 | { | |
498 | struct block_device *bdev; | |
499 | ||
500 | /* | |
501 | * Preserve backwards compatibility and allow large file access | |
502 | * even if userspace doesn't ask for it explicitly. Some mkfs | |
503 | * binary needs it. We might want to drop this workaround | |
504 | * during an unstable branch. | |
505 | */ | |
506 | filp->f_flags |= O_LARGEFILE; | |
a05f7bd9 | 507 | filp->f_mode |= FMODE_BUF_RASYNC | FMODE_CAN_ODIRECT; |
cd82cca7 | 508 | |
ee3249a8 CH |
509 | /* |
510 | * Use the file private data to store the holder for exclusive openes. | |
511 | * file_to_blk_mode relies on it being present to set BLK_OPEN_EXCL. | |
512 | */ | |
cd82cca7 | 513 | if (filp->f_flags & O_EXCL) |
ee3249a8 | 514 | filp->private_data = filp; |
cd82cca7 | 515 | |
05bdb996 | 516 | bdev = blkdev_get_by_dev(inode->i_rdev, file_to_blk_mode(filp), |
ee3249a8 | 517 | filp->private_data, NULL); |
cd82cca7 CH |
518 | if (IS_ERR(bdev)) |
519 | return PTR_ERR(bdev); | |
fac7c6d5 | 520 | |
e9833d87 JA |
521 | if (bdev_nowait(bdev)) |
522 | filp->f_mode |= FMODE_NOWAIT; | |
523 | ||
cd82cca7 CH |
524 | filp->f_mapping = bdev->bd_inode->i_mapping; |
525 | filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping); | |
526 | return 0; | |
527 | } | |
528 | ||
7ee34cbc | 529 | static int blkdev_release(struct inode *inode, struct file *filp) |
cd82cca7 | 530 | { |
ee3249a8 | 531 | blkdev_put(I_BDEV(filp->f_mapping->host), filp->private_data); |
cd82cca7 CH |
532 | return 0; |
533 | } | |
534 | ||
727cfe97 CH |
535 | static ssize_t |
536 | blkdev_direct_write(struct kiocb *iocb, struct iov_iter *from) | |
537 | { | |
538 | size_t count = iov_iter_count(from); | |
539 | ssize_t written; | |
540 | ||
541 | written = kiocb_invalidate_pages(iocb, count); | |
542 | if (written) { | |
543 | if (written == -EBUSY) | |
544 | return 0; | |
545 | return written; | |
546 | } | |
547 | ||
548 | written = blkdev_direct_IO(iocb, from); | |
549 | if (written > 0) { | |
550 | kiocb_invalidate_post_direct_write(iocb, count); | |
551 | iocb->ki_pos += written; | |
552 | count -= written; | |
553 | } | |
554 | if (written != -EIOCBQUEUED) | |
555 | iov_iter_revert(from, count - iov_iter_count(from)); | |
556 | return written; | |
557 | } | |
558 | ||
cd82cca7 CH |
559 | /* |
560 | * Write data to the block device. Only intended for the block device itself | |
561 | * and the raw driver which basically is a fake block device. | |
562 | * | |
563 | * Does not take i_mutex for the write and thus is not for general purpose | |
564 | * use. | |
565 | */ | |
566 | static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from) | |
567 | { | |
727cfe97 CH |
568 | struct file *file = iocb->ki_filp; |
569 | struct block_device *bdev = I_BDEV(file->f_mapping->host); | |
fac7c6d5 | 570 | struct inode *bd_inode = bdev->bd_inode; |
138c1a38 | 571 | loff_t size = bdev_nr_bytes(bdev); |
cd82cca7 CH |
572 | size_t shorted = 0; |
573 | ssize_t ret; | |
574 | ||
fac7c6d5 | 575 | if (bdev_read_only(bdev)) |
cd82cca7 CH |
576 | return -EPERM; |
577 | ||
578 | if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev)) | |
579 | return -ETXTBSY; | |
580 | ||
581 | if (!iov_iter_count(from)) | |
582 | return 0; | |
583 | ||
584 | if (iocb->ki_pos >= size) | |
585 | return -ENOSPC; | |
586 | ||
587 | if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT) | |
588 | return -EOPNOTSUPP; | |
589 | ||
590 | size -= iocb->ki_pos; | |
591 | if (iov_iter_count(from) > size) { | |
592 | shorted = iov_iter_count(from) - size; | |
593 | iov_iter_truncate(from, size); | |
594 | } | |
595 | ||
727cfe97 CH |
596 | ret = file_remove_privs(file); |
597 | if (ret) | |
598 | return ret; | |
599 | ||
600 | ret = file_update_time(file); | |
601 | if (ret) | |
602 | return ret; | |
603 | ||
604 | if (iocb->ki_flags & IOCB_DIRECT) { | |
605 | ret = blkdev_direct_write(iocb, from); | |
606 | if (ret >= 0 && iov_iter_count(from)) | |
607 | ret = direct_write_fallback(iocb, from, ret, | |
608 | generic_perform_write(iocb, from)); | |
609 | } else { | |
610 | ret = generic_perform_write(iocb, from); | |
611 | } | |
612 | ||
cd82cca7 CH |
613 | if (ret > 0) |
614 | ret = generic_write_sync(iocb, ret); | |
615 | iov_iter_reexpand(from, iov_iter_count(from) + shorted); | |
cd82cca7 CH |
616 | return ret; |
617 | } | |
618 | ||
619 | static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to) | |
620 | { | |
4e762d86 | 621 | struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host); |
138c1a38 | 622 | loff_t size = bdev_nr_bytes(bdev); |
cd82cca7 CH |
623 | loff_t pos = iocb->ki_pos; |
624 | size_t shorted = 0; | |
ceaa7625 | 625 | ssize_t ret = 0; |
3e1f941d | 626 | size_t count; |
cd82cca7 | 627 | |
3e1f941d | 628 | if (unlikely(pos + iov_iter_count(to) > size)) { |
6450fe1f PB |
629 | if (pos >= size) |
630 | return 0; | |
631 | size -= pos; | |
3e1f941d ID |
632 | shorted = iov_iter_count(to) - size; |
633 | iov_iter_truncate(to, size); | |
cd82cca7 CH |
634 | } |
635 | ||
3e1f941d ID |
636 | count = iov_iter_count(to); |
637 | if (!count) | |
638 | goto reexpand; /* skip atime */ | |
639 | ||
ceaa7625 | 640 | if (iocb->ki_flags & IOCB_DIRECT) { |
3c435a0f CH |
641 | ret = kiocb_write_and_wait(iocb, count); |
642 | if (ret < 0) | |
643 | goto reexpand; | |
ceaa7625 JA |
644 | file_accessed(iocb->ki_filp); |
645 | ||
646 | ret = blkdev_direct_IO(iocb, to); | |
647 | if (ret >= 0) { | |
648 | iocb->ki_pos += ret; | |
649 | count -= ret; | |
650 | } | |
3e1f941d | 651 | iov_iter_revert(to, count - iov_iter_count(to)); |
ceaa7625 | 652 | if (ret < 0 || !count) |
3e1f941d | 653 | goto reexpand; |
ceaa7625 JA |
654 | } |
655 | ||
656 | ret = filemap_read(iocb, to, ret); | |
6450fe1f | 657 | |
3e1f941d | 658 | reexpand: |
6450fe1f PB |
659 | if (unlikely(shorted)) |
660 | iov_iter_reexpand(to, iov_iter_count(to) + shorted); | |
cd82cca7 CH |
661 | return ret; |
662 | } | |
663 | ||
664 | #define BLKDEV_FALLOC_FL_SUPPORTED \ | |
665 | (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \ | |
666 | FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE) | |
667 | ||
668 | static long blkdev_fallocate(struct file *file, int mode, loff_t start, | |
669 | loff_t len) | |
670 | { | |
f278eb3d ML |
671 | struct inode *inode = bdev_file_inode(file); |
672 | struct block_device *bdev = I_BDEV(inode); | |
cd82cca7 CH |
673 | loff_t end = start + len - 1; |
674 | loff_t isize; | |
675 | int error; | |
676 | ||
677 | /* Fail if we don't recognize the flags. */ | |
678 | if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED) | |
679 | return -EOPNOTSUPP; | |
680 | ||
681 | /* Don't go off the end of the device. */ | |
2a93ad8f | 682 | isize = bdev_nr_bytes(bdev); |
cd82cca7 CH |
683 | if (start >= isize) |
684 | return -EINVAL; | |
685 | if (end >= isize) { | |
686 | if (mode & FALLOC_FL_KEEP_SIZE) { | |
687 | len = isize - start; | |
688 | end = start + len - 1; | |
689 | } else | |
690 | return -EINVAL; | |
691 | } | |
692 | ||
693 | /* | |
694 | * Don't allow IO that isn't aligned to logical block size. | |
695 | */ | |
696 | if ((start | len) & (bdev_logical_block_size(bdev) - 1)) | |
697 | return -EINVAL; | |
698 | ||
f278eb3d ML |
699 | filemap_invalidate_lock(inode->i_mapping); |
700 | ||
cd82cca7 | 701 | /* Invalidate the page cache, including dirty pages. */ |
05bdb996 | 702 | error = truncate_bdev_range(bdev, file_to_blk_mode(file), start, end); |
cd82cca7 | 703 | if (error) |
f278eb3d | 704 | goto fail; |
cd82cca7 CH |
705 | |
706 | switch (mode) { | |
707 | case FALLOC_FL_ZERO_RANGE: | |
708 | case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE: | |
6549a874 PB |
709 | error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT, |
710 | len >> SECTOR_SHIFT, GFP_KERNEL, | |
711 | BLKDEV_ZERO_NOUNMAP); | |
cd82cca7 CH |
712 | break; |
713 | case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE: | |
6549a874 PB |
714 | error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT, |
715 | len >> SECTOR_SHIFT, GFP_KERNEL, | |
716 | BLKDEV_ZERO_NOFALLBACK); | |
cd82cca7 CH |
717 | break; |
718 | case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE: | |
6549a874 | 719 | error = blkdev_issue_discard(bdev, start >> SECTOR_SHIFT, |
44abff2c | 720 | len >> SECTOR_SHIFT, GFP_KERNEL); |
cd82cca7 CH |
721 | break; |
722 | default: | |
f278eb3d | 723 | error = -EOPNOTSUPP; |
cd82cca7 | 724 | } |
cd82cca7 | 725 | |
f278eb3d ML |
726 | fail: |
727 | filemap_invalidate_unlock(inode->i_mapping); | |
728 | return error; | |
cd82cca7 CH |
729 | } |
730 | ||
69baa3a6 LP |
731 | static int blkdev_mmap(struct file *file, struct vm_area_struct *vma) |
732 | { | |
733 | struct inode *bd_inode = bdev_file_inode(file); | |
734 | ||
735 | if (bdev_read_only(I_BDEV(bd_inode))) | |
736 | return generic_file_readonly_mmap(file, vma); | |
737 | ||
738 | return generic_file_mmap(file, vma); | |
739 | } | |
740 | ||
cd82cca7 CH |
741 | const struct file_operations def_blk_fops = { |
742 | .open = blkdev_open, | |
7ee34cbc | 743 | .release = blkdev_release, |
cd82cca7 CH |
744 | .llseek = blkdev_llseek, |
745 | .read_iter = blkdev_read_iter, | |
746 | .write_iter = blkdev_write_iter, | |
3e08773c | 747 | .iopoll = iocb_bio_iopoll, |
69baa3a6 | 748 | .mmap = blkdev_mmap, |
cd82cca7 | 749 | .fsync = blkdev_fsync, |
8a709512 | 750 | .unlocked_ioctl = blkdev_ioctl, |
cd82cca7 CH |
751 | #ifdef CONFIG_COMPAT |
752 | .compat_ioctl = compat_blkdev_ioctl, | |
753 | #endif | |
2cb1e089 | 754 | .splice_read = filemap_splice_read, |
cd82cca7 CH |
755 | .splice_write = iter_file_splice_write, |
756 | .fallocate = blkdev_fallocate, | |
757 | }; | |
758 | ||
759 | static __init int blkdev_init(void) | |
760 | { | |
761 | return bioset_init(&blkdev_dio_pool, 4, | |
762 | offsetof(struct blkdev_dio, bio), | |
763 | BIOSET_NEED_BVECS|BIOSET_PERCPU_CACHE); | |
764 | } | |
765 | module_init(blkdev_init); |