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