xref: /linux/block/fops.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
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/blk-integrity.h>
11 #include <linux/buffer_head.h>
12 #include <linux/mpage.h>
13 #include <linux/uio.h>
14 #include <linux/namei.h>
15 #include <linux/task_io_accounting_ops.h>
16 #include <linux/falloc.h>
17 #include <linux/suspend.h>
18 #include <linux/fs.h>
19 #include <linux/iomap.h>
20 #include <linux/module.h>
21 #include <linux/io_uring/cmd.h>
22 #include <linux/dma-buf-io.h>
23 #include "blk.h"
24 
25 static inline struct inode *bdev_file_inode(struct file *file)
26 {
27 	return file->f_mapping->host;
28 }
29 
30 static bool blkdev_dio_fua(struct kiocb *iocb, struct block_device *bdev)
31 {
32 	if (!iocb_is_dsync(iocb))
33 		return false;
34 	/*
35 	 * Async writes cannot fall back to generic_write_sync(), so they must
36 	 * use FUA (emulated if needed); sync writes only need it when the
37 	 * device supports FUA natively.
38 	 */
39 	if (!is_sync_kiocb(iocb))
40 		return true;
41 	return bdev_fua(bdev);
42 }
43 
44 static blk_opf_t dio_bio_write_op(struct kiocb *iocb, struct block_device *bdev)
45 {
46 	blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
47 
48 	/* avoid the need for a I/O completion work item */
49 	if (blkdev_dio_fua(iocb, bdev))
50 		opf |= REQ_FUA;
51 	return opf;
52 }
53 
54 static bool blkdev_dio_invalid(struct block_device *bdev, struct kiocb *iocb,
55 				struct iov_iter *iter)
56 {
57 	return (iocb->ki_pos | iov_iter_count(iter)) &
58 			(bdev_logical_block_size(bdev) - 1);
59 }
60 
61 static inline int blkdev_iov_iter_get_pages(struct bio *bio,
62 		struct iov_iter *iter, struct block_device *bdev)
63 {
64 	return bio_iov_iter_get_pages(bio, iter, BIO_MAX_SIZE,
65 			bdev_dma_alignment(bdev),
66 			bdev_logical_block_size(bdev) - 1);
67 }
68 
69 #define DIO_INLINE_BIO_VECS 4
70 
71 static ssize_t __blkdev_direct_IO_simple(struct kiocb *iocb,
72 		struct iov_iter *iter, struct block_device *bdev,
73 		unsigned int nr_pages)
74 {
75 	struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
76 	loff_t pos = iocb->ki_pos;
77 	bool should_dirty = false;
78 	struct bio bio;
79 	ssize_t ret;
80 
81 	if (nr_pages <= DIO_INLINE_BIO_VECS)
82 		vecs = inline_vecs;
83 	else {
84 		vecs = kmalloc_objs(struct bio_vec, nr_pages);
85 		if (!vecs)
86 			return -ENOMEM;
87 	}
88 
89 	if (iov_iter_rw(iter) == READ) {
90 		bio_init(&bio, bdev, vecs, nr_pages, REQ_OP_READ);
91 		if (user_backed_iter(iter))
92 			should_dirty = true;
93 	} else {
94 		bio_init(&bio, bdev, vecs, nr_pages,
95 			 dio_bio_write_op(iocb, bdev));
96 	}
97 	bio.bi_iter.bi_sector = pos >> SECTOR_SHIFT;
98 	bio.bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint;
99 	bio.bi_write_stream = iocb->ki_write_stream;
100 	bio.bi_ioprio = iocb->ki_ioprio;
101 	if (iocb->ki_flags & IOCB_ATOMIC)
102 		bio.bi_opf |= REQ_ATOMIC;
103 
104 	ret = blkdev_iov_iter_get_pages(&bio, iter, bdev);
105 	if (unlikely(ret))
106 		goto out;
107 	ret = bio.bi_iter.bi_size;
108 
109 	if (iov_iter_rw(iter) == WRITE)
110 		task_io_account_write(ret);
111 
112 	if (iocb->ki_flags & IOCB_NOWAIT)
113 		bio.bi_opf |= REQ_NOWAIT;
114 
115 	submit_bio_wait(&bio);
116 
117 	bio_release_pages(&bio, should_dirty);
118 	if (unlikely(bio.bi_status))
119 		ret = blk_status_to_errno(bio.bi_status);
120 
121 out:
122 	if (vecs != inline_vecs)
123 		kfree(vecs);
124 
125 	bio_uninit(&bio);
126 
127 	return ret;
128 }
129 
130 enum {
131 	DIO_SHOULD_DIRTY	= 1,
132 	DIO_IS_SYNC		= 2,
133 };
134 
135 struct blkdev_dio {
136 	union {
137 		struct kiocb		*iocb;
138 		struct task_struct	*waiter;
139 	};
140 	size_t			size;
141 	atomic_t		ref;
142 	unsigned int		flags;
143 	struct bio		bio ____cacheline_aligned_in_smp;
144 };
145 
146 static struct bio_set blkdev_dio_pool;
147 
148 static void blkdev_bio_end_io(struct bio *bio)
149 {
150 	struct blkdev_dio *dio = bio->bi_private;
151 	bool should_dirty = dio->flags & DIO_SHOULD_DIRTY;
152 	bool is_sync = dio->flags & DIO_IS_SYNC;
153 
154 	if (bio->bi_status && !dio->bio.bi_status)
155 		dio->bio.bi_status = bio->bi_status;
156 
157 	if (bio_integrity(bio))
158 		bio_integrity_unmap_user(bio);
159 
160 	if (atomic_dec_and_test(&dio->ref)) {
161 		if (!is_sync) {
162 			struct kiocb *iocb = dio->iocb;
163 			ssize_t ret;
164 
165 			WRITE_ONCE(iocb->private, NULL);
166 
167 			if (likely(!dio->bio.bi_status)) {
168 				ret = dio->size;
169 				iocb->ki_pos += ret;
170 			} else {
171 				ret = blk_status_to_errno(dio->bio.bi_status);
172 			}
173 
174 			dio->iocb->ki_complete(iocb, ret);
175 			bio_put(&dio->bio);
176 		} else {
177 			struct task_struct *waiter = dio->waiter;
178 
179 			WRITE_ONCE(dio->waiter, NULL);
180 			blk_wake_io_task(waiter);
181 		}
182 	}
183 
184 	if (should_dirty) {
185 		bio_check_pages_dirty(bio);
186 	} else {
187 		bio_release_pages(bio, false);
188 		bio_put(bio);
189 	}
190 }
191 
192 static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
193 		struct block_device *bdev, unsigned int nr_pages)
194 {
195 	struct blk_plug plug;
196 	struct blkdev_dio *dio;
197 	struct bio *bio;
198 	bool is_read = (iov_iter_rw(iter) == READ), is_sync;
199 	blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb, bdev);
200 	loff_t pos = iocb->ki_pos;
201 	int ret = 0;
202 
203 	bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL,
204 			       &blkdev_dio_pool);
205 	dio = container_of(bio, struct blkdev_dio, bio);
206 	atomic_set(&dio->ref, 1);
207 	/*
208 	 * Grab an extra reference to ensure the dio structure which is embedded
209 	 * into the first bio stays around.
210 	 */
211 	bio_get(bio);
212 
213 	is_sync = is_sync_kiocb(iocb);
214 	if (is_sync) {
215 		dio->flags = DIO_IS_SYNC;
216 		dio->waiter = current;
217 	} else {
218 		dio->flags = 0;
219 		dio->iocb = iocb;
220 	}
221 
222 	dio->size = 0;
223 	if (is_read && user_backed_iter(iter))
224 		dio->flags |= DIO_SHOULD_DIRTY;
225 
226 	blk_start_plug(&plug);
227 
228 	for (;;) {
229 		bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
230 		bio->bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint;
231 		bio->bi_write_stream = iocb->ki_write_stream;
232 		bio->bi_private = dio;
233 		bio->bi_end_io = blkdev_bio_end_io;
234 		bio->bi_ioprio = iocb->ki_ioprio;
235 
236 		ret = blkdev_iov_iter_get_pages(bio, iter, bdev);
237 		if (unlikely(ret)) {
238 			bio_endio_status(bio, errno_to_blk_status(ret));
239 			break;
240 		}
241 		if (iocb->ki_flags & IOCB_NOWAIT) {
242 			/*
243 			 * This is nonblocking IO, and we need to allocate
244 			 * another bio if we have data left to map. As we
245 			 * cannot guarantee that one of the sub bios will not
246 			 * fail getting issued FOR NOWAIT and as error results
247 			 * are coalesced across all of them, be safe and ask for
248 			 * a retry of this from blocking context.
249 			 */
250 			if (unlikely(iov_iter_count(iter))) {
251 				ret = -EAGAIN;
252 				goto fail;
253 			}
254 			bio->bi_opf |= REQ_NOWAIT;
255 		}
256 		if (iocb->ki_flags & IOCB_HAS_METADATA) {
257 			ret = bio_integrity_map_iter(bio, iocb->private);
258 			if (unlikely(ret)) {
259 				bio_endio_status(bio, errno_to_blk_status(ret));
260 				break;
261 			}
262 		}
263 
264 		if (is_read) {
265 			if (dio->flags & DIO_SHOULD_DIRTY)
266 				bio_set_pages_dirty(bio);
267 		} else {
268 			task_io_account_write(bio->bi_iter.bi_size);
269 		}
270 		dio->size += bio->bi_iter.bi_size;
271 		pos += bio->bi_iter.bi_size;
272 
273 		nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS);
274 		if (!nr_pages) {
275 			submit_bio(bio);
276 			break;
277 		}
278 		atomic_inc(&dio->ref);
279 		submit_bio(bio);
280 		bio = bio_alloc(bdev, nr_pages, opf, GFP_KERNEL);
281 	}
282 
283 	blk_finish_plug(&plug);
284 
285 	if (!is_sync)
286 		return -EIOCBQUEUED;
287 
288 	for (;;) {
289 		set_current_state(TASK_UNINTERRUPTIBLE);
290 		if (!READ_ONCE(dio->waiter))
291 			break;
292 		blk_io_schedule();
293 	}
294 	__set_current_state(TASK_RUNNING);
295 
296 	if (!ret)
297 		ret = blk_status_to_errno(dio->bio.bi_status);
298 	if (likely(!ret))
299 		ret = dio->size;
300 
301 	bio_put(&dio->bio);
302 	return ret;
303 fail:
304 	bio_release_pages(bio, false);
305 	bio_clear_flag(bio, BIO_REFFED);
306 	bio_put(bio);
307 	blk_finish_plug(&plug);
308 	return ret;
309 }
310 
311 static void blkdev_bio_end_io_async(struct bio *bio)
312 {
313 	struct blkdev_dio *dio = container_of(bio, struct blkdev_dio, bio);
314 	struct kiocb *iocb = dio->iocb;
315 	ssize_t ret;
316 
317 	WRITE_ONCE(iocb->private, NULL);
318 
319 	if (likely(!bio->bi_status)) {
320 		ret = dio->size;
321 		iocb->ki_pos += ret;
322 	} else {
323 		ret = blk_status_to_errno(bio->bi_status);
324 	}
325 
326 	iocb->ki_complete(iocb, ret);
327 
328 	if (dio->flags & DIO_SHOULD_DIRTY) {
329 		bio_check_pages_dirty(bio);
330 	} else {
331 		bio_release_pages(bio, false);
332 		bio_put(bio);
333 	}
334 }
335 
336 static ssize_t __blkdev_direct_IO_async(struct kiocb *iocb,
337 					struct iov_iter *iter,
338 					struct block_device *bdev,
339 					unsigned int nr_pages)
340 {
341 	bool is_read = iov_iter_rw(iter) == READ;
342 	blk_opf_t opf = is_read ? REQ_OP_READ : dio_bio_write_op(iocb, bdev);
343 	struct blkdev_dio *dio;
344 	struct bio *bio;
345 	loff_t pos = iocb->ki_pos;
346 	int ret = 0;
347 
348 	bio = bio_alloc_bioset(bdev, nr_pages, opf, GFP_KERNEL,
349 			       &blkdev_dio_pool);
350 	dio = container_of(bio, struct blkdev_dio, bio);
351 	dio->flags = 0;
352 	dio->iocb = iocb;
353 	bio->bi_iter.bi_sector = pos >> SECTOR_SHIFT;
354 	bio->bi_write_hint = file_inode(iocb->ki_filp)->i_write_hint;
355 	bio->bi_write_stream = iocb->ki_write_stream;
356 	bio->bi_end_io = blkdev_bio_end_io_async;
357 	bio->bi_ioprio = iocb->ki_ioprio;
358 
359 	if (iocb->ki_flags & IOCB_ATOMIC)
360 		bio->bi_opf |= REQ_ATOMIC;
361 
362 	if (iocb->ki_flags & IOCB_NOWAIT)
363 		bio->bi_opf |= REQ_NOWAIT;
364 
365 	/*
366 	 * Users don't rely on the iterator being in any particular
367 	 * state for async I/O returning -EIOCBQUEUED, hence we can
368 	 * avoid expensive iov_iter_advance(). Bypass
369 	 * bio_iov_iter_get_pages() and set the bvec/dmabuf directly.
370 	 */
371 	if (!bio_iov_iter_set(bio, iter)) {
372 		ret = blkdev_iov_iter_get_pages(bio, iter, bdev);
373 		if (unlikely(ret))
374 			goto out_bio_put;
375 	}
376 	dio->size = bio->bi_iter.bi_size;
377 
378 	if (is_read) {
379 		if (user_backed_iter(iter)) {
380 			dio->flags |= DIO_SHOULD_DIRTY;
381 			bio_set_pages_dirty(bio);
382 		}
383 	} else {
384 		task_io_account_write(bio->bi_iter.bi_size);
385 	}
386 
387 	if (iocb->ki_flags & IOCB_HIPRI) {
388 		bio->bi_opf |= REQ_POLLED;
389 		submit_bio(bio);
390 		WRITE_ONCE(iocb->private, bio);
391 	} else {
392 		submit_bio(bio);
393 	}
394 	return -EIOCBQUEUED;
395 
396 out_bio_put:
397 	bio_put(bio);
398 	return ret;
399 }
400 
401 static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
402 {
403 	struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host);
404 	unsigned int nr_pages;
405 
406 	if (!iov_iter_count(iter))
407 		return 0;
408 
409 	if (blkdev_dio_invalid(bdev, iocb, iter))
410 		return -EINVAL;
411 
412 	/* HIPRI needs private as bio; HAS_METADATA keeps it as uio_meta */
413 	if ((iocb->ki_flags & IOCB_HIPRI) &&
414 	    (iocb->ki_flags & IOCB_HAS_METADATA))
415 		return -EOPNOTSUPP;
416 
417 	if (iov_iter_rw(iter) == WRITE) {
418 		u16 max_write_streams = bdev_max_write_streams(bdev);
419 
420 		if (iocb->ki_write_stream) {
421 			if (iocb->ki_write_stream > max_write_streams)
422 				return -EINVAL;
423 		} else if (max_write_streams) {
424 			enum rw_hint write_hint =
425 				file_inode(iocb->ki_filp)->i_write_hint;
426 
427 			/*
428 			 * Just use the write hint as write stream for block
429 			 * device writes.  This assumes no file system is
430 			 * mounted that would use the streams differently.
431 			 */
432 			if (write_hint <= max_write_streams)
433 				iocb->ki_write_stream = write_hint;
434 		}
435 	}
436 
437 	nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1);
438 	if (likely(nr_pages <= BIO_MAX_VECS &&
439 		   !(iocb->ki_flags & IOCB_HAS_METADATA))) {
440 		if (is_sync_kiocb(iocb))
441 			return __blkdev_direct_IO_simple(iocb, iter, bdev,
442 							nr_pages);
443 		return __blkdev_direct_IO_async(iocb, iter, bdev, nr_pages);
444 	} else if (iocb->ki_flags & IOCB_ATOMIC) {
445 		return -EINVAL;
446 	}
447 	return __blkdev_direct_IO(iocb, iter, bdev, bio_max_segs(nr_pages));
448 }
449 
450 static int blkdev_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
451 		unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
452 {
453 	struct block_device *bdev = I_BDEV(inode);
454 	loff_t isize = i_size_read(inode);
455 
456 	if (offset >= isize)
457 		return -EIO;
458 
459 	iomap->bdev = bdev;
460 	iomap->offset = ALIGN_DOWN(offset, bdev_logical_block_size(bdev));
461 	iomap->type = IOMAP_MAPPED;
462 	iomap->addr = iomap->offset;
463 	iomap->length = isize - iomap->offset;
464 	iomap->flags |= IOMAP_F_BUFFER_HEAD; /* noop for !CONFIG_BUFFER_HEAD */
465 	return 0;
466 }
467 
468 static DEFINE_IOMAP_ITER_NEXT(blkdev_iomap_next, blkdev_iomap_begin);
469 
470 #ifdef CONFIG_BUFFER_HEAD
471 static int blkdev_get_block(struct inode *inode, sector_t iblock,
472 		struct buffer_head *bh, int create)
473 {
474 	bh->b_bdev = I_BDEV(inode);
475 	bh->b_blocknr = iblock;
476 	set_buffer_mapped(bh);
477 	return 0;
478 }
479 
480 /*
481  * We cannot call mpage_writepages() as it does not take the buffer lock.
482  * We must use block_write_full_folio() directly which holds the buffer
483  * lock.  The buffer lock provides the synchronisation with writeback
484  * that filesystems rely on when they use the blockdev's mapping.
485  */
486 static int blkdev_writepages(struct address_space *mapping,
487 		struct writeback_control *wbc)
488 {
489 	struct folio *folio = NULL;
490 	struct blk_plug plug;
491 	int err;
492 
493 	blk_start_plug(&plug);
494 	while ((folio = writeback_iter(mapping, wbc, folio, &err)))
495 		err = block_write_full_folio(folio, wbc, blkdev_get_block);
496 	blk_finish_plug(&plug);
497 
498 	return err;
499 }
500 
501 static int blkdev_read_folio(struct file *file, struct folio *folio)
502 {
503 	return block_read_full_folio(folio, blkdev_get_block);
504 }
505 
506 static void blkdev_readahead(struct readahead_control *rac)
507 {
508 	mpage_readahead(rac, blkdev_get_block);
509 }
510 
511 const struct address_space_operations def_blk_aops = {
512 	.dirty_folio	= block_dirty_folio,
513 	.invalidate_folio = block_invalidate_folio,
514 	.read_folio	= blkdev_read_folio,
515 	.readahead	= blkdev_readahead,
516 	.writepages	= blkdev_writepages,
517 	.migrate_folio	= buffer_migrate_folio_norefs,
518 	.is_dirty_writeback = buffer_check_dirty_writeback,
519 };
520 #else /* CONFIG_BUFFER_HEAD */
521 static int blkdev_read_folio(struct file *file, struct folio *folio)
522 {
523 	iomap_bio_read_folio(folio, blkdev_iomap_next);
524 	return 0;
525 }
526 
527 static void blkdev_readahead(struct readahead_control *rac)
528 {
529 	iomap_bio_readahead(rac, blkdev_iomap_next);
530 }
531 
532 static ssize_t blkdev_writeback_range(struct iomap_writepage_ctx *wpc,
533 		struct folio *folio, u64 offset, unsigned int len, u64 end_pos)
534 {
535 	loff_t isize = i_size_read(wpc->inode);
536 
537 	if (WARN_ON_ONCE(offset >= isize))
538 		return -EIO;
539 
540 	if (offset < wpc->iomap.offset ||
541 	    offset >= wpc->iomap.offset + wpc->iomap.length) {
542 		int error;
543 
544 		error = blkdev_iomap_begin(wpc->inode, offset, isize - offset,
545 				IOMAP_WRITE, &wpc->iomap, NULL);
546 		if (error)
547 			return error;
548 	}
549 
550 	return iomap_add_to_ioend(wpc, folio, offset, end_pos, len);
551 }
552 
553 static const struct iomap_writeback_ops blkdev_writeback_ops = {
554 	.writeback_range	= blkdev_writeback_range,
555 	.writeback_submit	= iomap_ioend_writeback_submit,
556 };
557 
558 static int blkdev_writepages(struct address_space *mapping,
559 		struct writeback_control *wbc)
560 {
561 	struct iomap_writepage_ctx wpc = {
562 		.inode		= mapping->host,
563 		.wbc		= wbc,
564 		.ops		= &blkdev_writeback_ops
565 	};
566 
567 	return iomap_writepages(&wpc);
568 }
569 
570 const struct address_space_operations def_blk_aops = {
571 	.dirty_folio		= iomap_dirty_folio,
572 	.release_folio		= iomap_release_folio,
573 	.invalidate_folio	= iomap_invalidate_folio,
574 	.read_folio		= blkdev_read_folio,
575 	.readahead		= blkdev_readahead,
576 	.writepages		= blkdev_writepages,
577 	.is_partially_uptodate  = iomap_is_partially_uptodate,
578 	.error_remove_folio	= generic_error_remove_folio,
579 	.migrate_folio		= filemap_migrate_folio,
580 };
581 #endif /* CONFIG_BUFFER_HEAD */
582 
583 /*
584  * for a block special file file_inode(file)->i_size is zero
585  * so we compute the size by hand (just as in block_read/write above)
586  */
587 static loff_t blkdev_llseek(struct file *file, loff_t offset, int whence)
588 {
589 	struct inode *bd_inode = bdev_file_inode(file);
590 	loff_t retval;
591 
592 	inode_lock(bd_inode);
593 	retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
594 	inode_unlock(bd_inode);
595 	return retval;
596 }
597 
598 static int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
599 		int datasync)
600 {
601 	struct block_device *bdev = I_BDEV(filp->f_mapping->host);
602 	int error;
603 
604 	error = file_write_and_wait_range(filp, start, end);
605 	if (error)
606 		return error;
607 
608 	/*
609 	 * There is no need to serialise calls to blkdev_issue_flush with
610 	 * i_mutex and doing so causes performance issues with concurrent
611 	 * O_SYNC writers to a block device.
612 	 */
613 	error = blkdev_issue_flush(bdev);
614 	if (error == -EOPNOTSUPP)
615 		error = 0;
616 
617 	return error;
618 }
619 
620 /**
621  * file_to_blk_mode - get block open flags from file flags
622  * @file: file whose open flags should be converted
623  *
624  * Look at file open flags and generate corresponding block open flags from
625  * them. The function works both for file just being open (e.g. during ->open
626  * callback) and for file that is already open. This is actually non-trivial
627  * (see comment in the function).
628  */
629 blk_mode_t file_to_blk_mode(struct file *file)
630 {
631 	blk_mode_t mode = 0;
632 
633 	if (file->f_mode & FMODE_READ)
634 		mode |= BLK_OPEN_READ;
635 	if (file->f_mode & FMODE_WRITE)
636 		mode |= BLK_OPEN_WRITE;
637 	/*
638 	 * do_dentry_open() clears O_EXCL from f_flags, use file->private_data
639 	 * to determine whether the open was exclusive for already open files.
640 	 */
641 	if (file->private_data)
642 		mode |= BLK_OPEN_EXCL;
643 	else if (file->f_flags & O_EXCL)
644 		mode |= BLK_OPEN_EXCL;
645 	if (file->f_flags & O_NDELAY)
646 		mode |= BLK_OPEN_NDELAY;
647 
648 	/*
649 	 * If all bits in O_ACCMODE set (aka O_RDWR | O_WRONLY), the floppy
650 	 * driver has historically allowed ioctls as if the file was opened for
651 	 * writing, but does not allow and actual reads or writes.
652 	 */
653 	if ((file->f_flags & O_ACCMODE) == (O_RDWR | O_WRONLY))
654 		mode |= BLK_OPEN_WRITE_IOCTL;
655 
656 	return mode;
657 }
658 
659 static int blkdev_open(struct inode *inode, struct file *filp)
660 {
661 	struct block_device *bdev;
662 	blk_mode_t mode;
663 	int ret;
664 
665 	mode = file_to_blk_mode(filp);
666 	/* Use the file as the holder. */
667 	if (mode & BLK_OPEN_EXCL)
668 		filp->private_data = filp;
669 	ret = bdev_permission(inode->i_rdev, mode, filp->private_data);
670 	if (ret)
671 		return ret;
672 
673 	bdev = blkdev_get_no_open(inode->i_rdev, true);
674 	if (!bdev)
675 		return -ENXIO;
676 
677 	if (bdev_can_atomic_write(bdev))
678 		filp->f_mode |= FMODE_CAN_ATOMIC_WRITE;
679 	if (blk_get_integrity(bdev->bd_disk))
680 		filp->f_mode |= FMODE_HAS_METADATA;
681 
682 	ret = bdev_open(bdev, mode, filp->private_data, NULL, filp);
683 	if (ret)
684 		blkdev_put_no_open(bdev);
685 	return ret;
686 }
687 
688 static int blkdev_release(struct inode *inode, struct file *filp)
689 {
690 	bdev_release(filp);
691 	return 0;
692 }
693 
694 static ssize_t
695 blkdev_direct_write(struct kiocb *iocb, struct iov_iter *from)
696 {
697 	size_t count = iov_iter_count(from);
698 	ssize_t written;
699 
700 	written = kiocb_invalidate_pages(iocb, count);
701 	if (written) {
702 		if (written == -EBUSY)
703 			return 0;
704 		return written;
705 	}
706 
707 	written = blkdev_direct_IO(iocb, from);
708 	if (written > 0) {
709 		kiocb_invalidate_post_direct_write(iocb, count);
710 		iocb->ki_pos += written;
711 		count -= written;
712 	}
713 	if (written != -EIOCBQUEUED)
714 		iov_iter_revert(from, count - iov_iter_count(from));
715 	return written;
716 }
717 
718 static ssize_t blkdev_buffered_write(struct kiocb *iocb, struct iov_iter *from)
719 {
720 	struct inode *inode = bdev_file_inode(iocb->ki_filp);
721 	ssize_t ret;
722 
723 	/* Buffered fallback waits for writeback, which NOWAIT cannot allow. */
724 	if (iocb->ki_flags & IOCB_NOWAIT)
725 		return -EAGAIN;
726 
727 	if (unlikely(iov_iter_is_dmabuf_map(from)))
728 		return -EOPNOTSUPP;
729 
730 	/*
731 	 * Take i_rwsem to avoid racing with set_blocksize() changing
732 	 * i_blkbits/folio order and truncating the page cache.
733 	 */
734 	inode_lock_shared(inode);
735 	ret = iomap_file_buffered_write(iocb, from, blkdev_iomap_next, NULL,
736 					NULL);
737 	inode_unlock_shared(inode);
738 	return ret;
739 }
740 
741 /*
742  * Write data to the block device.  Only intended for the block device itself
743  * and the raw driver which basically is a fake block device.
744  *
745  * Does not take i_mutex for the write and thus is not for general purpose
746  * use.
747  */
748 static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
749 {
750 	struct file *file = iocb->ki_filp;
751 	struct inode *bd_inode = bdev_file_inode(file);
752 	struct block_device *bdev = I_BDEV(bd_inode);
753 	bool atomic = iocb->ki_flags & IOCB_ATOMIC;
754 	loff_t size = bdev_nr_bytes(bdev);
755 	size_t shorted = 0;
756 	bool need_sync = false;
757 	ssize_t ret;
758 
759 	if (bdev_read_only(bdev))
760 		return -EPERM;
761 
762 	if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
763 		return -ETXTBSY;
764 
765 	if (!iov_iter_count(from))
766 		return 0;
767 
768 	if (iocb->ki_pos >= size)
769 		return -ENOSPC;
770 
771 	if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
772 		return -EOPNOTSUPP;
773 
774 	if (atomic) {
775 		ret = generic_atomic_write_valid(iocb, from);
776 		if (ret)
777 			return ret;
778 	}
779 
780 	size -= iocb->ki_pos;
781 	if (iov_iter_count(from) > size) {
782 		if (atomic)
783 			return -EINVAL;
784 		shorted = iov_iter_count(from) - size;
785 		iov_iter_truncate(from, size);
786 	}
787 
788 	ret = file_update_time(file);
789 	if (ret)
790 		return ret;
791 
792 	if (iocb->ki_flags & IOCB_DIRECT) {
793 		ret = blkdev_direct_write(iocb, from);
794 		if (ret >= 0 && iov_iter_count(from)) {
795 			if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_ATOMIC)) {
796 				/*
797 				 * The buffered fallback blocks on i_rwsem and
798 				 * on writeback of the data it copied, and
799 				 * can't provide torn-write protection: return
800 				 * the short direct write instead and let the
801 				 * caller retry. The bytes already written
802 				 * still need a flush if REQ_FUA was not set.
803 				 */
804 				if (!ret)
805 					ret = -EAGAIN;
806 				else if (!blkdev_dio_fua(iocb, bdev))
807 					need_sync = true;
808 			} else {
809 				ssize_t ret2;
810 
811 				ret2 = blkdev_buffered_write(iocb, from);
812 				ret = direct_write_fallback(iocb, from, ret,
813 							    ret2);
814 				need_sync = true;
815 			}
816 		} else if (ret > 0 && !blkdev_dio_fua(iocb, bdev)) {
817 			/*
818 			 * The device does not support FUA, so REQ_FUA was not
819 			 * set and the O_DSYNC direct write still needs a flush
820 			 * to be made durable.
821 			 */
822 			need_sync = true;
823 		}
824 	} else {
825 		ret = blkdev_buffered_write(iocb, from);
826 		need_sync = true;
827 	}
828 
829 	if (ret > 0 && need_sync)
830 		ret = generic_write_sync(iocb, ret);
831 	iov_iter_reexpand(from, iov_iter_count(from) + shorted);
832 	return ret;
833 }
834 
835 static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
836 {
837 	struct inode *bd_inode = bdev_file_inode(iocb->ki_filp);
838 	struct block_device *bdev = I_BDEV(iocb->ki_filp->f_mapping->host);
839 	loff_t size = bdev_nr_bytes(bdev);
840 	loff_t pos = iocb->ki_pos;
841 	size_t shorted = 0;
842 	ssize_t ret = 0;
843 	size_t count;
844 
845 	if (unlikely(pos + iov_iter_count(to) > size)) {
846 		if (pos >= size)
847 			return 0;
848 		size -= pos;
849 		shorted = iov_iter_count(to) - size;
850 		iov_iter_truncate(to, size);
851 	}
852 
853 	count = iov_iter_count(to);
854 	if (!count)
855 		goto reexpand; /* skip atime */
856 
857 	if (iocb->ki_flags & IOCB_DIRECT) {
858 		ret = kiocb_write_and_wait(iocb, count);
859 		if (ret < 0)
860 			goto reexpand;
861 		file_accessed(iocb->ki_filp);
862 
863 		ret = blkdev_direct_IO(iocb, to);
864 		if (ret > 0) {
865 			iocb->ki_pos += ret;
866 			count -= ret;
867 		}
868 		if (ret != -EIOCBQUEUED)
869 			iov_iter_revert(to, count - iov_iter_count(to));
870 		if (ret < 0 || !count)
871 			goto reexpand;
872 	}
873 	if (unlikely(iov_iter_is_dmabuf_map(to))) {
874 		if (!ret)
875 			ret = -EOPNOTSUPP;
876 		goto reexpand;
877 	}
878 
879 	/*
880 	 * Take i_rwsem and invalidate_lock to avoid racing with set_blocksize
881 	 * changing i_blkbits/folio order and punching out the pagecache.
882 	 */
883 	if (iocb->ki_flags & IOCB_NOWAIT) {
884 		if (!inode_trylock_shared(bd_inode)) {
885 			if (!ret)
886 				ret = -EAGAIN;
887 			goto reexpand;
888 		}
889 	} else {
890 		inode_lock_shared(bd_inode);
891 	}
892 	ret = filemap_read(iocb, to, ret);
893 	inode_unlock_shared(bd_inode);
894 
895 reexpand:
896 	if (unlikely(shorted))
897 		iov_iter_reexpand(to, iov_iter_count(to) + shorted);
898 	return ret;
899 }
900 
901 /*
902  * Take i_rwsem to avoid racing with set_blocksize changing i_blkbits/folio
903  * order and punching out the pagecache.
904  */
905 static ssize_t blkdev_splice_read(struct file *in, loff_t *ppos,
906 		struct pipe_inode_info *pipe, size_t len, unsigned int flags)
907 {
908 	struct inode *bd_inode = bdev_file_inode(in);
909 	ssize_t ret;
910 
911 	inode_lock_shared(bd_inode);
912 	ret = filemap_splice_read(in, ppos, pipe, len, flags);
913 	inode_unlock_shared(bd_inode);
914 	return ret;
915 }
916 
917 #define	BLKDEV_FALLOC_FL_SUPPORTED					\
918 		(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |		\
919 		 FALLOC_FL_ZERO_RANGE | FALLOC_FL_WRITE_ZEROES)
920 
921 static long blkdev_fallocate(struct file *file, int mode, loff_t start,
922 			     loff_t len)
923 {
924 	struct inode *inode = bdev_file_inode(file);
925 	struct block_device *bdev = I_BDEV(inode);
926 	loff_t end = start + len - 1;
927 	loff_t isize;
928 	unsigned int flags;
929 	int error;
930 
931 	/* Fail if we don't recognize the flags. */
932 	if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
933 		return -EOPNOTSUPP;
934 	/*
935 	 * Don't allow writing zeroes if the device does not enable the
936 	 * unmap write zeroes operation.
937 	 */
938 	if ((mode & FALLOC_FL_WRITE_ZEROES) &&
939 	    !bdev_write_zeroes_unmap_sectors(bdev))
940 		return -EOPNOTSUPP;
941 
942 	/* Don't go off the end of the device. */
943 	isize = bdev_nr_bytes(bdev);
944 	if (start >= isize)
945 		return -EINVAL;
946 	if (end >= isize) {
947 		if (mode & FALLOC_FL_KEEP_SIZE) {
948 			len = isize - start;
949 			end = start + len - 1;
950 		} else
951 			return -EINVAL;
952 	}
953 
954 	/*
955 	 * Don't allow IO that isn't aligned to logical block size.
956 	 */
957 	if ((start | len) & (bdev_logical_block_size(bdev) - 1))
958 		return -EINVAL;
959 
960 	inode_lock(inode);
961 	filemap_invalidate_lock(inode->i_mapping);
962 
963 	switch (mode) {
964 	case FALLOC_FL_ZERO_RANGE:
965 	case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
966 		flags = BLKDEV_ZERO_NOUNMAP;
967 		break;
968 	case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
969 		flags = BLKDEV_ZERO_NOFALLBACK;
970 		break;
971 	case FALLOC_FL_WRITE_ZEROES:
972 		flags = 0;
973 		break;
974 	default:
975 		error = -EOPNOTSUPP;
976 		goto fail;
977 	}
978 
979 	/*
980 	 * Invalidate the page cache, including dirty pages, for valid
981 	 * de-allocate mode calls to fallocate().
982 	 */
983 	error = truncate_bdev_range(bdev, file_to_blk_mode(file), start, end);
984 	if (error)
985 		goto fail;
986 
987 	error = blkdev_issue_zeroout(bdev, start >> SECTOR_SHIFT,
988 				     len >> SECTOR_SHIFT, GFP_KERNEL, flags);
989  fail:
990 	filemap_invalidate_unlock(inode->i_mapping);
991 	inode_unlock(inode);
992 	return error;
993 }
994 
995 static int blkdev_mmap_prepare(struct vm_area_desc *desc)
996 {
997 	struct file *file = desc->file;
998 
999 	if (bdev_read_only(I_BDEV(bdev_file_inode(file))))
1000 		return generic_file_readonly_mmap_prepare(desc);
1001 
1002 	return generic_file_mmap_prepare(desc);
1003 }
1004 
1005 static int blkdev_init_dma_buf_io_ctx(struct file *file,
1006 				      struct dma_buf_io_ctx *ctx)
1007 {
1008 	struct block_device *bdev = file_bdev(file);
1009 	struct gendisk *disk = bdev->bd_disk;
1010 
1011 	if (!(file->f_flags & O_DIRECT))
1012 		return -EINVAL;
1013 	if (ctx->dmabuf->size > UINT_MAX)
1014 		return -EINVAL;
1015 	if (!disk->fops->init_dma_buf_io_ctx)
1016 		return -EOPNOTSUPP;
1017 	return disk->fops->init_dma_buf_io_ctx(bdev, ctx);
1018 }
1019 
1020 const struct file_operations def_blk_fops = {
1021 	.open		= blkdev_open,
1022 	.release	= blkdev_release,
1023 	.llseek		= blkdev_llseek,
1024 	.read_iter	= blkdev_read_iter,
1025 	.write_iter	= blkdev_write_iter,
1026 	.iopoll		= iocb_bio_iopoll,
1027 	.mmap_prepare	= blkdev_mmap_prepare,
1028 	.fsync		= blkdev_fsync,
1029 	.unlocked_ioctl	= blkdev_ioctl,
1030 #ifdef CONFIG_COMPAT
1031 	.compat_ioctl	= compat_blkdev_ioctl,
1032 #endif
1033 	.splice_read	= blkdev_splice_read,
1034 	.splice_write	= iter_file_splice_write,
1035 	.fallocate	= blkdev_fallocate,
1036 	.uring_cmd	= blkdev_uring_cmd,
1037 	.fop_flags	= FOP_BUFFER_RASYNC | FOP_DONTCACHE,
1038 	.init_dma_buf_io_ctx = blkdev_init_dma_buf_io_ctx,
1039 };
1040 
1041 static __init int blkdev_init(void)
1042 {
1043 	return bioset_init(&blkdev_dio_pool, 4,
1044 				offsetof(struct blkdev_dio, bio),
1045 				BIOSET_NEED_BVECS|BIOSET_PERCPU_CACHE);
1046 }
1047 module_init(blkdev_init);
1048