fops.c 17 KB

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