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