4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
11 * linux/fs/minix/file.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * ext4 fs regular file handling primitives
17 * 64-bit file support on 64-bit platforms by Jakub Jelinek
18 * (jj@sunsite.ms.mff.cuni.cz)
21 #include <linux/time.h>
23 #include <linux/mount.h>
24 #include <linux/path.h>
25 #include <linux/dax.h>
26 #include <linux/quotaops.h>
27 #include <linux/pagevec.h>
28 #include <linux/uio.h>
30 #include "ext4_jbd2.h"
35 static ssize_t
ext4_dax_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
37 struct inode
*inode
= file_inode(iocb
->ki_filp
);
40 if (!inode_trylock_shared(inode
)) {
41 if (iocb
->ki_flags
& IOCB_NOWAIT
)
43 inode_lock_shared(inode
);
46 * Recheck under inode lock - at this point we are sure it cannot
50 inode_unlock_shared(inode
);
51 /* Fallback to buffered IO in case we cannot support DAX */
52 return generic_file_read_iter(iocb
, to
);
54 ret
= dax_iomap_rw(iocb
, to
, &ext4_iomap_ops
);
55 inode_unlock_shared(inode
);
57 file_accessed(iocb
->ki_filp
);
62 static ssize_t
ext4_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
64 if (unlikely(ext4_forced_shutdown(EXT4_SB(file_inode(iocb
->ki_filp
)->i_sb
))))
67 if (!iov_iter_count(to
))
68 return 0; /* skip atime */
71 if (IS_DAX(file_inode(iocb
->ki_filp
)))
72 return ext4_dax_read_iter(iocb
, to
);
74 return generic_file_read_iter(iocb
, to
);
78 * Called when an inode is released. Note that this is different
79 * from ext4_file_open: open gets called at every open, but release
80 * gets called only when /all/ the files are closed.
82 static int ext4_release_file(struct inode
*inode
, struct file
*filp
)
84 if (ext4_test_inode_state(inode
, EXT4_STATE_DA_ALLOC_CLOSE
)) {
85 ext4_alloc_da_blocks(inode
);
86 ext4_clear_inode_state(inode
, EXT4_STATE_DA_ALLOC_CLOSE
);
88 /* if we are the last writer on the inode, drop the block reservation */
89 if ((filp
->f_mode
& FMODE_WRITE
) &&
90 (atomic_read(&inode
->i_writecount
) == 1) &&
91 !EXT4_I(inode
)->i_reserved_data_blocks
)
93 down_write(&EXT4_I(inode
)->i_data_sem
);
94 ext4_discard_preallocations(inode
);
95 up_write(&EXT4_I(inode
)->i_data_sem
);
97 if (is_dx(inode
) && filp
->private_data
)
98 ext4_htree_free_dir_info(filp
->private_data
);
103 static void ext4_unwritten_wait(struct inode
*inode
)
105 wait_queue_head_t
*wq
= ext4_ioend_wq(inode
);
107 wait_event(*wq
, (atomic_read(&EXT4_I(inode
)->i_unwritten
) == 0));
111 * This tests whether the IO in question is block-aligned or not.
112 * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
113 * are converted to written only after the IO is complete. Until they are
114 * mapped, these blocks appear as holes, so dio_zero_block() will assume that
115 * it needs to zero out portions of the start and/or end block. If 2 AIO
116 * threads are at work on the same unwritten block, they must be synchronized
117 * or one thread will zero the other's data, causing corruption.
120 ext4_unaligned_aio(struct inode
*inode
, struct iov_iter
*from
, loff_t pos
)
122 struct super_block
*sb
= inode
->i_sb
;
123 int blockmask
= sb
->s_blocksize
- 1;
125 if (pos
>= i_size_read(inode
))
128 if ((pos
| iov_iter_alignment(from
)) & blockmask
)
134 /* Is IO overwriting allocated and initialized blocks? */
135 static bool ext4_overwrite_io(struct inode
*inode
, loff_t pos
, loff_t len
)
137 struct ext4_map_blocks map
;
138 unsigned int blkbits
= inode
->i_blkbits
;
141 if (pos
+ len
> i_size_read(inode
))
144 map
.m_lblk
= pos
>> blkbits
;
145 map
.m_len
= EXT4_MAX_BLOCKS(len
, pos
, blkbits
);
148 err
= ext4_map_blocks(NULL
, inode
, &map
, 0);
150 * 'err==len' means that all of the blocks have been preallocated,
151 * regardless of whether they have been initialized or not. To exclude
152 * unwritten extents, we need to check m_flags.
154 return err
== blklen
&& (map
.m_flags
& EXT4_MAP_MAPPED
);
157 static ssize_t
ext4_write_checks(struct kiocb
*iocb
, struct iov_iter
*from
)
159 struct inode
*inode
= file_inode(iocb
->ki_filp
);
162 ret
= generic_write_checks(iocb
, from
);
166 * If we have encountered a bitmap-format file, the size limit
167 * is smaller than s_maxbytes, which is for extent-mapped files.
169 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))) {
170 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
172 if (iocb
->ki_pos
>= sbi
->s_bitmap_maxbytes
)
174 iov_iter_truncate(from
, sbi
->s_bitmap_maxbytes
- iocb
->ki_pos
);
176 return iov_iter_count(from
);
181 ext4_dax_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
183 struct inode
*inode
= file_inode(iocb
->ki_filp
);
186 if (!inode_trylock(inode
)) {
187 if (iocb
->ki_flags
& IOCB_NOWAIT
)
191 ret
= ext4_write_checks(iocb
, from
);
194 ret
= file_remove_privs(iocb
->ki_filp
);
197 ret
= file_update_time(iocb
->ki_filp
);
201 ret
= dax_iomap_rw(iocb
, from
, &ext4_iomap_ops
);
205 ret
= generic_write_sync(iocb
, ret
);
211 ext4_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
213 struct inode
*inode
= file_inode(iocb
->ki_filp
);
214 int o_direct
= iocb
->ki_flags
& IOCB_DIRECT
;
215 int unaligned_aio
= 0;
219 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
224 return ext4_dax_write_iter(iocb
, from
);
226 if (!o_direct
&& (iocb
->ki_flags
& IOCB_NOWAIT
))
229 if (!inode_trylock(inode
)) {
230 if (iocb
->ki_flags
& IOCB_NOWAIT
)
235 ret
= ext4_write_checks(iocb
, from
);
240 * Unaligned direct AIO must be serialized among each other as zeroing
241 * of partial blocks of two competing unaligned AIOs can result in data
244 if (o_direct
&& ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
) &&
245 !is_sync_kiocb(iocb
) &&
246 ext4_unaligned_aio(inode
, from
, iocb
->ki_pos
)) {
248 ext4_unwritten_wait(inode
);
251 iocb
->private = &overwrite
;
252 /* Check whether we do a DIO overwrite or not */
253 if (o_direct
&& !unaligned_aio
) {
254 if (ext4_overwrite_io(inode
, iocb
->ki_pos
, iov_iter_count(from
))) {
255 if (ext4_should_dioread_nolock(inode
))
257 } else if (iocb
->ki_flags
& IOCB_NOWAIT
) {
263 ret
= __generic_file_write_iter(iocb
, from
);
267 ret
= generic_write_sync(iocb
, ret
);
277 static int ext4_dax_huge_fault(struct vm_fault
*vmf
,
278 enum page_entry_size pe_size
)
281 handle_t
*handle
= NULL
;
282 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
283 struct super_block
*sb
= inode
->i_sb
;
286 * We have to distinguish real writes from writes which will result in a
287 * COW page; COW writes should *not* poke the journal (the file will not
288 * be changed). Doing so would cause unintended failures when mounted
291 * We check for VM_SHARED rather than vmf->cow_page since the latter is
292 * unset for pe_size != PE_SIZE_PTE (i.e. only in do_cow_fault); for
293 * other sizes, dax_iomap_fault will handle splitting / fallback so that
294 * we eventually come back with a COW page.
296 bool write
= (vmf
->flags
& FAULT_FLAG_WRITE
) &&
297 (vmf
->vma
->vm_flags
& VM_SHARED
);
300 sb_start_pagefault(sb
);
301 file_update_time(vmf
->vma
->vm_file
);
302 down_read(&EXT4_I(inode
)->i_mmap_sem
);
303 handle
= ext4_journal_start_sb(sb
, EXT4_HT_WRITE_PAGE
,
304 EXT4_DATA_TRANS_BLOCKS(sb
));
306 down_read(&EXT4_I(inode
)->i_mmap_sem
);
309 result
= dax_iomap_fault(vmf
, pe_size
, &ext4_iomap_ops
);
311 result
= VM_FAULT_SIGBUS
;
314 ext4_journal_stop(handle
);
315 up_read(&EXT4_I(inode
)->i_mmap_sem
);
316 sb_end_pagefault(sb
);
318 up_read(&EXT4_I(inode
)->i_mmap_sem
);
324 static int ext4_dax_fault(struct vm_fault
*vmf
)
326 return ext4_dax_huge_fault(vmf
, PE_SIZE_PTE
);
329 static const struct vm_operations_struct ext4_dax_vm_ops
= {
330 .fault
= ext4_dax_fault
,
331 .huge_fault
= ext4_dax_huge_fault
,
332 .page_mkwrite
= ext4_dax_fault
,
333 .pfn_mkwrite
= ext4_dax_fault
,
336 #define ext4_dax_vm_ops ext4_file_vm_ops
339 static const struct vm_operations_struct ext4_file_vm_ops
= {
340 .fault
= ext4_filemap_fault
,
341 .map_pages
= filemap_map_pages
,
342 .page_mkwrite
= ext4_page_mkwrite
,
345 static int ext4_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
347 struct inode
*inode
= file
->f_mapping
->host
;
349 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
353 if (IS_DAX(file_inode(file
))) {
354 vma
->vm_ops
= &ext4_dax_vm_ops
;
355 vma
->vm_flags
|= VM_MIXEDMAP
| VM_HUGEPAGE
;
357 vma
->vm_ops
= &ext4_file_vm_ops
;
362 static int ext4_file_open(struct inode
* inode
, struct file
* filp
)
364 struct super_block
*sb
= inode
->i_sb
;
365 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
366 struct vfsmount
*mnt
= filp
->f_path
.mnt
;
372 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
375 if (unlikely(!(sbi
->s_mount_flags
& EXT4_MF_MNTDIR_SAMPLED
) &&
377 sbi
->s_mount_flags
|= EXT4_MF_MNTDIR_SAMPLED
;
379 * Sample where the filesystem has been mounted and
380 * store it in the superblock for sysadmin convenience
381 * when trying to sort through large numbers of block
382 * devices or filesystem images.
384 memset(buf
, 0, sizeof(buf
));
386 path
.dentry
= mnt
->mnt_root
;
387 cp
= d_path(&path
, buf
, sizeof(buf
));
392 handle
= ext4_journal_start_sb(sb
, EXT4_HT_MISC
, 1);
394 return PTR_ERR(handle
);
395 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
396 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
398 ext4_journal_stop(handle
);
401 strlcpy(sbi
->s_es
->s_last_mounted
, cp
,
402 sizeof(sbi
->s_es
->s_last_mounted
));
403 ext4_handle_dirty_super(handle
, sb
);
404 ext4_journal_stop(handle
);
407 if (ext4_encrypted_inode(inode
)) {
408 ret
= fscrypt_get_encryption_info(inode
);
411 if (!fscrypt_has_encryption_key(inode
))
415 dir
= dget_parent(file_dentry(filp
));
416 if (ext4_encrypted_inode(d_inode(dir
)) &&
417 !fscrypt_has_permitted_context(d_inode(dir
), inode
)) {
418 ext4_warning(inode
->i_sb
,
419 "Inconsistent encryption contexts: %lu/%lu",
420 (unsigned long) d_inode(dir
)->i_ino
,
421 (unsigned long) inode
->i_ino
);
427 * Set up the jbd2_inode if we are opening the inode for
428 * writing and the journal is present
430 if (filp
->f_mode
& FMODE_WRITE
) {
431 ret
= ext4_inode_attach_jinode(inode
);
436 filp
->f_mode
|= FMODE_NOWAIT
;
437 return dquot_file_open(inode
, filp
);
441 * Here we use ext4_map_blocks() to get a block mapping for a extent-based
442 * file rather than ext4_ext_walk_space() because we can introduce
443 * SEEK_DATA/SEEK_HOLE for block-mapped and extent-mapped file at the same
444 * function. When extent status tree has been fully implemented, it will
445 * track all extent status for a file and we can directly use it to
446 * retrieve the offset for SEEK_DATA/SEEK_HOLE.
450 * When we retrieve the offset for SEEK_DATA/SEEK_HOLE, we would need to
451 * lookup page cache to check whether or not there has some data between
452 * [startoff, endoff] because, if this range contains an unwritten extent,
453 * we determine this extent as a data or a hole according to whether the
454 * page cache has data or not.
456 static int ext4_find_unwritten_pgoff(struct inode
*inode
,
462 unsigned int blkbits
;
470 blkbits
= inode
->i_sb
->s_blocksize_bits
;
473 endoff
= (loff_t
)end_blk
<< blkbits
;
475 index
= startoff
>> PAGE_SHIFT
;
476 end
= (endoff
- 1) >> PAGE_SHIFT
;
478 pagevec_init(&pvec
, 0);
481 unsigned long nr_pages
;
483 nr_pages
= pagevec_lookup_range(&pvec
, inode
->i_mapping
,
488 for (i
= 0; i
< nr_pages
; i
++) {
489 struct page
*page
= pvec
.pages
[i
];
490 struct buffer_head
*bh
, *head
;
493 * If current offset is smaller than the page offset,
494 * there is a hole at this offset.
496 if (whence
== SEEK_HOLE
&& lastoff
< endoff
&&
497 lastoff
< page_offset(pvec
.pages
[i
])) {
505 if (unlikely(page
->mapping
!= inode
->i_mapping
)) {
510 if (!page_has_buffers(page
)) {
515 if (page_has_buffers(page
)) {
516 lastoff
= page_offset(page
);
517 bh
= head
= page_buffers(page
);
519 if (lastoff
+ bh
->b_size
<= startoff
)
521 if (buffer_uptodate(bh
) ||
522 buffer_unwritten(bh
)) {
523 if (whence
== SEEK_DATA
)
526 if (whence
== SEEK_HOLE
)
530 *offset
= max_t(loff_t
,
536 lastoff
+= bh
->b_size
;
537 bh
= bh
->b_this_page
;
538 } while (bh
!= head
);
541 lastoff
= page_offset(page
) + PAGE_SIZE
;
545 pagevec_release(&pvec
);
546 } while (index
<= end
);
548 /* There are no pages upto endoff - that would be a hole in there. */
549 if (whence
== SEEK_HOLE
&& lastoff
< endoff
) {
554 pagevec_release(&pvec
);
559 * ext4_seek_data() retrieves the offset for SEEK_DATA.
561 static loff_t
ext4_seek_data(struct file
*file
, loff_t offset
, loff_t maxsize
)
563 struct inode
*inode
= file
->f_mapping
->host
;
564 struct extent_status es
;
565 ext4_lblk_t start
, last
, end
;
566 loff_t dataoff
, isize
;
572 isize
= i_size_read(inode
);
573 if (offset
< 0 || offset
>= isize
) {
578 blkbits
= inode
->i_sb
->s_blocksize_bits
;
579 start
= offset
>> blkbits
;
581 end
= isize
>> blkbits
;
585 ret
= ext4_get_next_extent(inode
, last
, end
- last
+ 1, &es
);
587 /* No extent found -> no data */
596 dataoff
= (loff_t
)last
<< blkbits
;
597 if (!ext4_es_is_unwritten(&es
))
601 * If there is a unwritten extent at this offset,
602 * it will be as a data or a hole according to page
603 * cache that has data or not.
605 if (ext4_find_unwritten_pgoff(inode
, SEEK_DATA
,
606 es
.es_lblk
+ es
.es_len
, &dataoff
))
609 dataoff
= (loff_t
)last
<< blkbits
;
611 } while (last
<= end
);
618 return vfs_setpos(file
, dataoff
, maxsize
);
622 * ext4_seek_hole() retrieves the offset for SEEK_HOLE.
624 static loff_t
ext4_seek_hole(struct file
*file
, loff_t offset
, loff_t maxsize
)
626 struct inode
*inode
= file
->f_mapping
->host
;
627 struct extent_status es
;
628 ext4_lblk_t start
, last
, end
;
629 loff_t holeoff
, isize
;
635 isize
= i_size_read(inode
);
636 if (offset
< 0 || offset
>= isize
) {
641 blkbits
= inode
->i_sb
->s_blocksize_bits
;
642 start
= offset
>> blkbits
;
644 end
= isize
>> blkbits
;
648 ret
= ext4_get_next_extent(inode
, last
, end
- last
+ 1, &es
);
654 if (ret
== 0 || es
.es_lblk
> last
) {
656 holeoff
= (loff_t
)last
<< blkbits
;
660 * If there is a unwritten extent at this offset,
661 * it will be as a data or a hole according to page
662 * cache that has data or not.
664 if (ext4_es_is_unwritten(&es
) &&
665 ext4_find_unwritten_pgoff(inode
, SEEK_HOLE
,
666 last
+ es
.es_len
, &holeoff
))
670 holeoff
= (loff_t
)last
<< blkbits
;
672 } while (last
<= end
);
679 return vfs_setpos(file
, holeoff
, maxsize
);
683 * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
684 * by calling generic_file_llseek_size() with the appropriate maxbytes
687 loff_t
ext4_llseek(struct file
*file
, loff_t offset
, int whence
)
689 struct inode
*inode
= file
->f_mapping
->host
;
692 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
693 maxbytes
= EXT4_SB(inode
->i_sb
)->s_bitmap_maxbytes
;
695 maxbytes
= inode
->i_sb
->s_maxbytes
;
701 return generic_file_llseek_size(file
, offset
, whence
,
702 maxbytes
, i_size_read(inode
));
704 return ext4_seek_data(file
, offset
, maxbytes
);
706 return ext4_seek_hole(file
, offset
, maxbytes
);
712 const struct file_operations ext4_file_operations
= {
713 .llseek
= ext4_llseek
,
714 .read_iter
= ext4_file_read_iter
,
715 .write_iter
= ext4_file_write_iter
,
716 .unlocked_ioctl
= ext4_ioctl
,
718 .compat_ioctl
= ext4_compat_ioctl
,
720 .mmap
= ext4_file_mmap
,
721 .open
= ext4_file_open
,
722 .release
= ext4_release_file
,
723 .fsync
= ext4_sync_file
,
724 .get_unmapped_area
= thp_get_unmapped_area
,
725 .splice_read
= generic_file_splice_read
,
726 .splice_write
= iter_file_splice_write
,
727 .fallocate
= ext4_fallocate
,
730 const struct inode_operations ext4_file_inode_operations
= {
731 .setattr
= ext4_setattr
,
732 .getattr
= ext4_file_getattr
,
733 .listxattr
= ext4_listxattr
,
734 .get_acl
= ext4_get_acl
,
735 .set_acl
= ext4_set_acl
,
736 .fiemap
= ext4_fiemap
,