GNU Linux-libre 4.14.266-gnu1
[releases.git] / fs / ext4 / file.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/ext4/file.c
4  *
5  * Copyright (C) 1992, 1993, 1994, 1995
6  * Remy Card (card@masi.ibp.fr)
7  * Laboratoire MASI - Institut Blaise Pascal
8  * Universite Pierre et Marie Curie (Paris VI)
9  *
10  *  from
11  *
12  *  linux/fs/minix/file.c
13  *
14  *  Copyright (C) 1991, 1992  Linus Torvalds
15  *
16  *  ext4 fs regular file handling primitives
17  *
18  *  64-bit file support on 64-bit platforms by Jakub Jelinek
19  *      (jj@sunsite.ms.mff.cuni.cz)
20  */
21
22 #include <linux/time.h>
23 #include <linux/fs.h>
24 #include <linux/mount.h>
25 #include <linux/path.h>
26 #include <linux/dax.h>
27 #include <linux/quotaops.h>
28 #include <linux/pagevec.h>
29 #include <linux/uio.h>
30 #include "ext4.h"
31 #include "ext4_jbd2.h"
32 #include "xattr.h"
33 #include "acl.h"
34
35 #ifdef CONFIG_FS_DAX
36 static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
37 {
38         struct inode *inode = file_inode(iocb->ki_filp);
39         ssize_t ret;
40
41         if (iocb->ki_flags & IOCB_NOWAIT) {
42                 if (!inode_trylock_shared(inode))
43                         return -EAGAIN;
44         } else {
45                 inode_lock_shared(inode);
46         }
47         /*
48          * Recheck under inode lock - at this point we are sure it cannot
49          * change anymore
50          */
51         if (!IS_DAX(inode)) {
52                 inode_unlock_shared(inode);
53                 /* Fallback to buffered IO in case we cannot support DAX */
54                 return generic_file_read_iter(iocb, to);
55         }
56         ret = dax_iomap_rw(iocb, to, &ext4_iomap_ops);
57         inode_unlock_shared(inode);
58
59         file_accessed(iocb->ki_filp);
60         return ret;
61 }
62 #endif
63
64 static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
65 {
66         if (unlikely(ext4_forced_shutdown(EXT4_SB(file_inode(iocb->ki_filp)->i_sb))))
67                 return -EIO;
68
69         if (!iov_iter_count(to))
70                 return 0; /* skip atime */
71
72 #ifdef CONFIG_FS_DAX
73         if (IS_DAX(file_inode(iocb->ki_filp)))
74                 return ext4_dax_read_iter(iocb, to);
75 #endif
76         return generic_file_read_iter(iocb, to);
77 }
78
79 /*
80  * Called when an inode is released. Note that this is different
81  * from ext4_file_open: open gets called at every open, but release
82  * gets called only when /all/ the files are closed.
83  */
84 static int ext4_release_file(struct inode *inode, struct file *filp)
85 {
86         if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) {
87                 ext4_alloc_da_blocks(inode);
88                 ext4_clear_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
89         }
90         /* if we are the last writer on the inode, drop the block reservation */
91         if ((filp->f_mode & FMODE_WRITE) &&
92                         (atomic_read(&inode->i_writecount) == 1) &&
93                         !EXT4_I(inode)->i_reserved_data_blocks)
94         {
95                 down_write(&EXT4_I(inode)->i_data_sem);
96                 ext4_discard_preallocations(inode);
97                 up_write(&EXT4_I(inode)->i_data_sem);
98         }
99         if (is_dx(inode) && filp->private_data)
100                 ext4_htree_free_dir_info(filp->private_data);
101
102         return 0;
103 }
104
105 static void ext4_unwritten_wait(struct inode *inode)
106 {
107         wait_queue_head_t *wq = ext4_ioend_wq(inode);
108
109         wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_unwritten) == 0));
110 }
111
112 /*
113  * This tests whether the IO in question is block-aligned or not.
114  * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
115  * are converted to written only after the IO is complete.  Until they are
116  * mapped, these blocks appear as holes, so dio_zero_block() will assume that
117  * it needs to zero out portions of the start and/or end block.  If 2 AIO
118  * threads are at work on the same unwritten block, they must be synchronized
119  * or one thread will zero the other's data, causing corruption.
120  */
121 static int
122 ext4_unaligned_aio(struct inode *inode, struct iov_iter *from, loff_t pos)
123 {
124         struct super_block *sb = inode->i_sb;
125         int blockmask = sb->s_blocksize - 1;
126
127         if (pos >= ALIGN(i_size_read(inode), sb->s_blocksize))
128                 return 0;
129
130         if ((pos | iov_iter_alignment(from)) & blockmask)
131                 return 1;
132
133         return 0;
134 }
135
136 /* Is IO overwriting allocated and initialized blocks? */
137 static bool ext4_overwrite_io(struct inode *inode, loff_t pos, loff_t len)
138 {
139         struct ext4_map_blocks map;
140         unsigned int blkbits = inode->i_blkbits;
141         int err, blklen;
142
143         if (pos + len > i_size_read(inode))
144                 return false;
145
146         map.m_lblk = pos >> blkbits;
147         map.m_len = EXT4_MAX_BLOCKS(len, pos, blkbits);
148         blklen = map.m_len;
149
150         err = ext4_map_blocks(NULL, inode, &map, 0);
151         /*
152          * 'err==len' means that all of the blocks have been preallocated,
153          * regardless of whether they have been initialized or not. To exclude
154          * unwritten extents, we need to check m_flags.
155          */
156         return err == blklen && (map.m_flags & EXT4_MAP_MAPPED);
157 }
158
159 static ssize_t ext4_write_checks(struct kiocb *iocb, struct iov_iter *from)
160 {
161         struct inode *inode = file_inode(iocb->ki_filp);
162         ssize_t ret;
163
164         ret = generic_write_checks(iocb, from);
165         if (ret <= 0)
166                 return ret;
167
168         if (unlikely(IS_IMMUTABLE(inode)))
169                 return -EPERM;
170
171         /*
172          * If we have encountered a bitmap-format file, the size limit
173          * is smaller than s_maxbytes, which is for extent-mapped files.
174          */
175         if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
176                 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
177
178                 if (iocb->ki_pos >= sbi->s_bitmap_maxbytes)
179                         return -EFBIG;
180                 iov_iter_truncate(from, sbi->s_bitmap_maxbytes - iocb->ki_pos);
181         }
182         return iov_iter_count(from);
183 }
184
185 #ifdef CONFIG_FS_DAX
186 static ssize_t
187 ext4_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
188 {
189         struct inode *inode = file_inode(iocb->ki_filp);
190         ssize_t ret;
191
192         if (iocb->ki_flags & IOCB_NOWAIT) {
193                 if (!inode_trylock(inode))
194                         return -EAGAIN;
195         } else {
196                 inode_lock(inode);
197         }
198         ret = ext4_write_checks(iocb, from);
199         if (ret <= 0)
200                 goto out;
201         ret = file_remove_privs(iocb->ki_filp);
202         if (ret)
203                 goto out;
204         ret = file_update_time(iocb->ki_filp);
205         if (ret)
206                 goto out;
207
208         ret = dax_iomap_rw(iocb, from, &ext4_iomap_ops);
209 out:
210         inode_unlock(inode);
211         if (ret > 0)
212                 ret = generic_write_sync(iocb, ret);
213         return ret;
214 }
215 #endif
216
217 static ssize_t
218 ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
219 {
220         struct inode *inode = file_inode(iocb->ki_filp);
221         int o_direct = iocb->ki_flags & IOCB_DIRECT;
222         int unaligned_aio = 0;
223         int overwrite = 0;
224         ssize_t ret;
225
226         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
227                 return -EIO;
228
229 #ifdef CONFIG_FS_DAX
230         if (IS_DAX(inode))
231                 return ext4_dax_write_iter(iocb, from);
232 #endif
233         if (!o_direct && (iocb->ki_flags & IOCB_NOWAIT))
234                 return -EOPNOTSUPP;
235
236         if (!inode_trylock(inode)) {
237                 if (iocb->ki_flags & IOCB_NOWAIT)
238                         return -EAGAIN;
239                 inode_lock(inode);
240         }
241
242         ret = ext4_write_checks(iocb, from);
243         if (ret <= 0)
244                 goto out;
245
246         /*
247          * Unaligned direct AIO must be serialized among each other as zeroing
248          * of partial blocks of two competing unaligned AIOs can result in data
249          * corruption.
250          */
251         if (o_direct && ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) &&
252             !is_sync_kiocb(iocb) &&
253             ext4_unaligned_aio(inode, from, iocb->ki_pos)) {
254                 unaligned_aio = 1;
255                 ext4_unwritten_wait(inode);
256         }
257
258         iocb->private = &overwrite;
259         /* Check whether we do a DIO overwrite or not */
260         if (o_direct && !unaligned_aio) {
261                 if (ext4_overwrite_io(inode, iocb->ki_pos, iov_iter_count(from))) {
262                         if (ext4_should_dioread_nolock(inode))
263                                 overwrite = 1;
264                 } else if (iocb->ki_flags & IOCB_NOWAIT) {
265                         ret = -EAGAIN;
266                         goto out;
267                 }
268         }
269
270         ret = __generic_file_write_iter(iocb, from);
271         /*
272          * Unaligned direct AIO must be the only IO in flight. Otherwise
273          * overlapping aligned IO after unaligned might result in data
274          * corruption.
275          */
276         if (ret == -EIOCBQUEUED && unaligned_aio)
277                 ext4_unwritten_wait(inode);
278         inode_unlock(inode);
279
280         if (ret > 0)
281                 ret = generic_write_sync(iocb, ret);
282
283         return ret;
284
285 out:
286         inode_unlock(inode);
287         return ret;
288 }
289
290 #ifdef CONFIG_FS_DAX
291 static int ext4_dax_huge_fault(struct vm_fault *vmf,
292                 enum page_entry_size pe_size)
293 {
294         int result;
295         handle_t *handle = NULL;
296         struct inode *inode = file_inode(vmf->vma->vm_file);
297         struct super_block *sb = inode->i_sb;
298
299         /*
300          * We have to distinguish real writes from writes which will result in a
301          * COW page; COW writes should *not* poke the journal (the file will not
302          * be changed). Doing so would cause unintended failures when mounted
303          * read-only.
304          *
305          * We check for VM_SHARED rather than vmf->cow_page since the latter is
306          * unset for pe_size != PE_SIZE_PTE (i.e. only in do_cow_fault); for
307          * other sizes, dax_iomap_fault will handle splitting / fallback so that
308          * we eventually come back with a COW page.
309          */
310         bool write = (vmf->flags & FAULT_FLAG_WRITE) &&
311                 (vmf->vma->vm_flags & VM_SHARED);
312
313         if (write) {
314                 sb_start_pagefault(sb);
315                 file_update_time(vmf->vma->vm_file);
316                 down_read(&EXT4_I(inode)->i_mmap_sem);
317                 handle = ext4_journal_start_sb(sb, EXT4_HT_WRITE_PAGE,
318                                                EXT4_DATA_TRANS_BLOCKS(sb));
319         } else {
320                 down_read(&EXT4_I(inode)->i_mmap_sem);
321         }
322         if (!IS_ERR(handle))
323                 result = dax_iomap_fault(vmf, pe_size, &ext4_iomap_ops);
324         else
325                 result = VM_FAULT_SIGBUS;
326         if (write) {
327                 if (!IS_ERR(handle))
328                         ext4_journal_stop(handle);
329                 up_read(&EXT4_I(inode)->i_mmap_sem);
330                 sb_end_pagefault(sb);
331         } else {
332                 up_read(&EXT4_I(inode)->i_mmap_sem);
333         }
334
335         return result;
336 }
337
338 static int ext4_dax_fault(struct vm_fault *vmf)
339 {
340         return ext4_dax_huge_fault(vmf, PE_SIZE_PTE);
341 }
342
343 static const struct vm_operations_struct ext4_dax_vm_ops = {
344         .fault          = ext4_dax_fault,
345         .huge_fault     = ext4_dax_huge_fault,
346         .page_mkwrite   = ext4_dax_fault,
347         .pfn_mkwrite    = ext4_dax_fault,
348 };
349 #else
350 #define ext4_dax_vm_ops ext4_file_vm_ops
351 #endif
352
353 static const struct vm_operations_struct ext4_file_vm_ops = {
354         .fault          = ext4_filemap_fault,
355         .map_pages      = filemap_map_pages,
356         .page_mkwrite   = ext4_page_mkwrite,
357 };
358
359 static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
360 {
361         struct inode *inode = file->f_mapping->host;
362
363         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
364                 return -EIO;
365
366         file_accessed(file);
367         if (IS_DAX(file_inode(file))) {
368                 vma->vm_ops = &ext4_dax_vm_ops;
369                 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
370         } else {
371                 vma->vm_ops = &ext4_file_vm_ops;
372         }
373         return 0;
374 }
375
376 static int ext4_file_open(struct inode * inode, struct file * filp)
377 {
378         struct super_block *sb = inode->i_sb;
379         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
380         struct vfsmount *mnt = filp->f_path.mnt;
381         struct dentry *dir;
382         struct path path;
383         char buf[64], *cp;
384         int ret;
385
386         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
387                 return -EIO;
388
389         if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
390                      !sb_rdonly(sb))) {
391                 sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
392                 /*
393                  * Sample where the filesystem has been mounted and
394                  * store it in the superblock for sysadmin convenience
395                  * when trying to sort through large numbers of block
396                  * devices or filesystem images.
397                  */
398                 memset(buf, 0, sizeof(buf));
399                 path.mnt = mnt;
400                 path.dentry = mnt->mnt_root;
401                 cp = d_path(&path, buf, sizeof(buf));
402                 if (!IS_ERR(cp)) {
403                         handle_t *handle;
404                         int err;
405
406                         handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
407                         if (IS_ERR(handle))
408                                 return PTR_ERR(handle);
409                         BUFFER_TRACE(sbi->s_sbh, "get_write_access");
410                         err = ext4_journal_get_write_access(handle, sbi->s_sbh);
411                         if (err) {
412                                 ext4_journal_stop(handle);
413                                 return err;
414                         }
415                         strlcpy(sbi->s_es->s_last_mounted, cp,
416                                 sizeof(sbi->s_es->s_last_mounted));
417                         ext4_handle_dirty_super(handle, sb);
418                         ext4_journal_stop(handle);
419                 }
420         }
421         if (ext4_encrypted_inode(inode)) {
422                 ret = fscrypt_get_encryption_info(inode);
423                 if (ret)
424                         return -EACCES;
425                 if (!fscrypt_has_encryption_key(inode))
426                         return -ENOKEY;
427         }
428
429         dir = dget_parent(file_dentry(filp));
430         if (ext4_encrypted_inode(d_inode(dir)) &&
431                         !fscrypt_has_permitted_context(d_inode(dir), inode)) {
432                 ext4_warning(inode->i_sb,
433                              "Inconsistent encryption contexts: %lu/%lu",
434                              (unsigned long) d_inode(dir)->i_ino,
435                              (unsigned long) inode->i_ino);
436                 dput(dir);
437                 return -EPERM;
438         }
439         dput(dir);
440         /*
441          * Set up the jbd2_inode if we are opening the inode for
442          * writing and the journal is present
443          */
444         if (filp->f_mode & FMODE_WRITE) {
445                 ret = ext4_inode_attach_jinode(inode);
446                 if (ret < 0)
447                         return ret;
448         }
449
450         filp->f_mode |= FMODE_NOWAIT;
451         return dquot_file_open(inode, filp);
452 }
453
454 /*
455  * Here we use ext4_map_blocks() to get a block mapping for a extent-based
456  * file rather than ext4_ext_walk_space() because we can introduce
457  * SEEK_DATA/SEEK_HOLE for block-mapped and extent-mapped file at the same
458  * function.  When extent status tree has been fully implemented, it will
459  * track all extent status for a file and we can directly use it to
460  * retrieve the offset for SEEK_DATA/SEEK_HOLE.
461  */
462
463 /*
464  * When we retrieve the offset for SEEK_DATA/SEEK_HOLE, we would need to
465  * lookup page cache to check whether or not there has some data between
466  * [startoff, endoff] because, if this range contains an unwritten extent,
467  * we determine this extent as a data or a hole according to whether the
468  * page cache has data or not.
469  */
470 static int ext4_find_unwritten_pgoff(struct inode *inode,
471                                      int whence,
472                                      ext4_lblk_t end_blk,
473                                      loff_t *offset)
474 {
475         struct pagevec pvec;
476         unsigned int blkbits;
477         pgoff_t index;
478         pgoff_t end;
479         loff_t endoff;
480         loff_t startoff;
481         loff_t lastoff;
482         int found = 0;
483
484         blkbits = inode->i_sb->s_blocksize_bits;
485         startoff = *offset;
486         lastoff = startoff;
487         endoff = (loff_t)end_blk << blkbits;
488
489         index = startoff >> PAGE_SHIFT;
490         end = (endoff - 1) >> PAGE_SHIFT;
491
492         pagevec_init(&pvec, 0);
493         do {
494                 int i;
495                 unsigned long nr_pages;
496
497                 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping,
498                                         &index, end);
499                 if (nr_pages == 0)
500                         break;
501
502                 for (i = 0; i < nr_pages; i++) {
503                         struct page *page = pvec.pages[i];
504                         struct buffer_head *bh, *head;
505
506                         /*
507                          * If current offset is smaller than the page offset,
508                          * there is a hole at this offset.
509                          */
510                         if (whence == SEEK_HOLE && lastoff < endoff &&
511                             lastoff < page_offset(pvec.pages[i])) {
512                                 found = 1;
513                                 *offset = lastoff;
514                                 goto out;
515                         }
516
517                         lock_page(page);
518
519                         if (unlikely(page->mapping != inode->i_mapping)) {
520                                 unlock_page(page);
521                                 continue;
522                         }
523
524                         if (!page_has_buffers(page)) {
525                                 unlock_page(page);
526                                 continue;
527                         }
528
529                         if (page_has_buffers(page)) {
530                                 lastoff = page_offset(page);
531                                 bh = head = page_buffers(page);
532                                 do {
533                                         if (lastoff + bh->b_size <= startoff)
534                                                 goto next;
535                                         if (buffer_uptodate(bh) ||
536                                             buffer_unwritten(bh)) {
537                                                 if (whence == SEEK_DATA)
538                                                         found = 1;
539                                         } else {
540                                                 if (whence == SEEK_HOLE)
541                                                         found = 1;
542                                         }
543                                         if (found) {
544                                                 *offset = max_t(loff_t,
545                                                         startoff, lastoff);
546                                                 unlock_page(page);
547                                                 goto out;
548                                         }
549 next:
550                                         lastoff += bh->b_size;
551                                         bh = bh->b_this_page;
552                                 } while (bh != head);
553                         }
554
555                         lastoff = page_offset(page) + PAGE_SIZE;
556                         unlock_page(page);
557                 }
558
559                 pagevec_release(&pvec);
560         } while (index <= end);
561
562         /* There are no pages upto endoff - that would be a hole in there. */
563         if (whence == SEEK_HOLE && lastoff < endoff) {
564                 found = 1;
565                 *offset = lastoff;
566         }
567 out:
568         pagevec_release(&pvec);
569         return found;
570 }
571
572 /*
573  * ext4_seek_data() retrieves the offset for SEEK_DATA.
574  */
575 static loff_t ext4_seek_data(struct file *file, loff_t offset, loff_t maxsize)
576 {
577         struct inode *inode = file->f_mapping->host;
578         struct extent_status es;
579         ext4_lblk_t start, last, end;
580         loff_t dataoff, isize;
581         int blkbits;
582         int ret;
583
584         inode_lock(inode);
585
586         isize = i_size_read(inode);
587         if (offset < 0 || offset >= isize) {
588                 inode_unlock(inode);
589                 return -ENXIO;
590         }
591
592         blkbits = inode->i_sb->s_blocksize_bits;
593         start = offset >> blkbits;
594         last = start;
595         end = isize >> blkbits;
596         dataoff = offset;
597
598         do {
599                 ret = ext4_get_next_extent(inode, last, end - last + 1, &es);
600                 if (ret <= 0) {
601                         /* No extent found -> no data */
602                         if (ret == 0)
603                                 ret = -ENXIO;
604                         inode_unlock(inode);
605                         return ret;
606                 }
607
608                 last = es.es_lblk;
609                 if (last != start)
610                         dataoff = (loff_t)last << blkbits;
611                 if (!ext4_es_is_unwritten(&es))
612                         break;
613
614                 /*
615                  * If there is a unwritten extent at this offset,
616                  * it will be as a data or a hole according to page
617                  * cache that has data or not.
618                  */
619                 if (ext4_find_unwritten_pgoff(inode, SEEK_DATA,
620                                               es.es_lblk + es.es_len, &dataoff))
621                         break;
622                 last += es.es_len;
623                 dataoff = (loff_t)last << blkbits;
624                 cond_resched();
625         } while (last <= end);
626
627         inode_unlock(inode);
628
629         if (dataoff > isize)
630                 return -ENXIO;
631
632         return vfs_setpos(file, dataoff, maxsize);
633 }
634
635 /*
636  * ext4_seek_hole() retrieves the offset for SEEK_HOLE.
637  */
638 static loff_t ext4_seek_hole(struct file *file, loff_t offset, loff_t maxsize)
639 {
640         struct inode *inode = file->f_mapping->host;
641         struct extent_status es;
642         ext4_lblk_t start, last, end;
643         loff_t holeoff, isize;
644         int blkbits;
645         int ret;
646
647         inode_lock(inode);
648
649         isize = i_size_read(inode);
650         if (offset < 0 || offset >= isize) {
651                 inode_unlock(inode);
652                 return -ENXIO;
653         }
654
655         blkbits = inode->i_sb->s_blocksize_bits;
656         start = offset >> blkbits;
657         last = start;
658         end = isize >> blkbits;
659         holeoff = offset;
660
661         do {
662                 ret = ext4_get_next_extent(inode, last, end - last + 1, &es);
663                 if (ret < 0) {
664                         inode_unlock(inode);
665                         return ret;
666                 }
667                 /* Found a hole? */
668                 if (ret == 0 || es.es_lblk > last) {
669                         if (last != start)
670                                 holeoff = (loff_t)last << blkbits;
671                         break;
672                 }
673                 /*
674                  * If there is a unwritten extent at this offset,
675                  * it will be as a data or a hole according to page
676                  * cache that has data or not.
677                  */
678                 if (ext4_es_is_unwritten(&es) &&
679                     ext4_find_unwritten_pgoff(inode, SEEK_HOLE,
680                                               last + es.es_len, &holeoff))
681                         break;
682
683                 last += es.es_len;
684                 holeoff = (loff_t)last << blkbits;
685                 cond_resched();
686         } while (last <= end);
687
688         inode_unlock(inode);
689
690         if (holeoff > isize)
691                 holeoff = isize;
692
693         return vfs_setpos(file, holeoff, maxsize);
694 }
695
696 /*
697  * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
698  * by calling generic_file_llseek_size() with the appropriate maxbytes
699  * value for each.
700  */
701 loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
702 {
703         struct inode *inode = file->f_mapping->host;
704         loff_t maxbytes;
705
706         if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
707                 maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
708         else
709                 maxbytes = inode->i_sb->s_maxbytes;
710
711         switch (whence) {
712         case SEEK_SET:
713         case SEEK_CUR:
714         case SEEK_END:
715                 return generic_file_llseek_size(file, offset, whence,
716                                                 maxbytes, i_size_read(inode));
717         case SEEK_DATA:
718                 return ext4_seek_data(file, offset, maxbytes);
719         case SEEK_HOLE:
720                 return ext4_seek_hole(file, offset, maxbytes);
721         }
722
723         return -EINVAL;
724 }
725
726 const struct file_operations ext4_file_operations = {
727         .llseek         = ext4_llseek,
728         .read_iter      = ext4_file_read_iter,
729         .write_iter     = ext4_file_write_iter,
730         .unlocked_ioctl = ext4_ioctl,
731 #ifdef CONFIG_COMPAT
732         .compat_ioctl   = ext4_compat_ioctl,
733 #endif
734         .mmap           = ext4_file_mmap,
735         .open           = ext4_file_open,
736         .release        = ext4_release_file,
737         .fsync          = ext4_sync_file,
738         .get_unmapped_area = thp_get_unmapped_area,
739         .splice_read    = generic_file_splice_read,
740         .splice_write   = iter_file_splice_write,
741         .fallocate      = ext4_fallocate,
742 };
743
744 const struct inode_operations ext4_file_inode_operations = {
745         .setattr        = ext4_setattr,
746         .getattr        = ext4_file_getattr,
747         .listxattr      = ext4_listxattr,
748         .get_acl        = ext4_get_acl,
749         .set_acl        = ext4_set_acl,
750         .fiemap         = ext4_fiemap,
751 };
752