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 * Called when an inode is released. Note that this is different
36 * from ext4_file_open: open gets called at every open, but release
37 * gets called only when /all/ the files are closed.
39 static int ext4_release_file(struct inode
*inode
, struct file
*filp
)
41 if (ext4_test_inode_state(inode
, EXT4_STATE_DA_ALLOC_CLOSE
)) {
42 ext4_alloc_da_blocks(inode
);
43 ext4_clear_inode_state(inode
, EXT4_STATE_DA_ALLOC_CLOSE
);
45 /* if we are the last writer on the inode, drop the block reservation */
46 if ((filp
->f_mode
& FMODE_WRITE
) &&
47 (atomic_read(&inode
->i_writecount
) == 1) &&
48 !EXT4_I(inode
)->i_reserved_data_blocks
)
50 down_write(&EXT4_I(inode
)->i_data_sem
);
51 ext4_discard_preallocations(inode
);
52 up_write(&EXT4_I(inode
)->i_data_sem
);
54 if (is_dx(inode
) && filp
->private_data
)
55 ext4_htree_free_dir_info(filp
->private_data
);
60 static void ext4_unwritten_wait(struct inode
*inode
)
62 wait_queue_head_t
*wq
= ext4_ioend_wq(inode
);
64 wait_event(*wq
, (atomic_read(&EXT4_I(inode
)->i_unwritten
) == 0));
68 * This tests whether the IO in question is block-aligned or not.
69 * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
70 * are converted to written only after the IO is complete. Until they are
71 * mapped, these blocks appear as holes, so dio_zero_block() will assume that
72 * it needs to zero out portions of the start and/or end block. If 2 AIO
73 * threads are at work on the same unwritten block, they must be synchronized
74 * or one thread will zero the other's data, causing corruption.
77 ext4_unaligned_aio(struct inode
*inode
, struct iov_iter
*from
, loff_t pos
)
79 struct super_block
*sb
= inode
->i_sb
;
80 int blockmask
= sb
->s_blocksize
- 1;
82 if (pos
>= i_size_read(inode
))
85 if ((pos
| iov_iter_alignment(from
)) & blockmask
)
92 ext4_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
94 struct inode
*inode
= file_inode(iocb
->ki_filp
);
95 int o_direct
= iocb
->ki_flags
& IOCB_DIRECT
;
96 int unaligned_aio
= 0;
101 ret
= generic_write_checks(iocb
, from
);
106 * Unaligned direct AIO must be serialized among each other as zeroing
107 * of partial blocks of two competing unaligned AIOs can result in data
110 if (o_direct
&& ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
) &&
111 !is_sync_kiocb(iocb
) &&
112 ext4_unaligned_aio(inode
, from
, iocb
->ki_pos
)) {
114 ext4_unwritten_wait(inode
);
118 * If we have encountered a bitmap-format file, the size limit
119 * is smaller than s_maxbytes, which is for extent-mapped files.
121 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
))) {
122 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
124 if (iocb
->ki_pos
>= sbi
->s_bitmap_maxbytes
) {
128 iov_iter_truncate(from
, sbi
->s_bitmap_maxbytes
- iocb
->ki_pos
);
131 iocb
->private = &overwrite
;
133 size_t length
= iov_iter_count(from
);
134 loff_t pos
= iocb
->ki_pos
;
136 /* check whether we do a DIO overwrite or not */
137 if (ext4_should_dioread_nolock(inode
) && !unaligned_aio
&&
138 pos
+ length
<= i_size_read(inode
)) {
139 struct ext4_map_blocks map
;
140 unsigned int blkbits
= inode
->i_blkbits
;
143 map
.m_lblk
= pos
>> blkbits
;
144 map
.m_len
= EXT4_MAX_BLOCKS(length
, pos
, blkbits
);
147 err
= ext4_map_blocks(NULL
, inode
, &map
, 0);
149 * 'err==len' means that all of blocks has
150 * been preallocated no matter they are
151 * initialized or not. For excluding
152 * unwritten extents, we need to check
153 * m_flags. There are two conditions that
154 * indicate for initialized extents. 1) If we
155 * hit extent cache, EXT4_MAP_MAPPED flag is
156 * returned; 2) If we do a real lookup,
157 * non-flags are returned. So we should check
158 * these two conditions.
160 if (err
== len
&& (map
.m_flags
& EXT4_MAP_MAPPED
))
165 ret
= __generic_file_write_iter(iocb
, from
);
169 ret
= generic_write_sync(iocb
, ret
);
179 static int ext4_dax_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
182 handle_t
*handle
= NULL
;
183 struct inode
*inode
= file_inode(vma
->vm_file
);
184 struct super_block
*sb
= inode
->i_sb
;
185 bool write
= vmf
->flags
& FAULT_FLAG_WRITE
;
188 sb_start_pagefault(sb
);
189 file_update_time(vma
->vm_file
);
190 down_read(&EXT4_I(inode
)->i_mmap_sem
);
191 handle
= ext4_journal_start_sb(sb
, EXT4_HT_WRITE_PAGE
,
192 EXT4_DATA_TRANS_BLOCKS(sb
));
194 down_read(&EXT4_I(inode
)->i_mmap_sem
);
197 result
= VM_FAULT_SIGBUS
;
199 result
= dax_fault(vma
, vmf
, ext4_dax_get_block
);
203 ext4_journal_stop(handle
);
204 up_read(&EXT4_I(inode
)->i_mmap_sem
);
205 sb_end_pagefault(sb
);
207 up_read(&EXT4_I(inode
)->i_mmap_sem
);
212 static int ext4_dax_pmd_fault(struct vm_area_struct
*vma
, unsigned long addr
,
213 pmd_t
*pmd
, unsigned int flags
)
216 handle_t
*handle
= NULL
;
217 struct inode
*inode
= file_inode(vma
->vm_file
);
218 struct super_block
*sb
= inode
->i_sb
;
219 bool write
= flags
& FAULT_FLAG_WRITE
;
222 sb_start_pagefault(sb
);
223 file_update_time(vma
->vm_file
);
224 down_read(&EXT4_I(inode
)->i_mmap_sem
);
225 handle
= ext4_journal_start_sb(sb
, EXT4_HT_WRITE_PAGE
,
226 ext4_chunk_trans_blocks(inode
,
227 PMD_SIZE
/ PAGE_SIZE
));
229 down_read(&EXT4_I(inode
)->i_mmap_sem
);
232 result
= VM_FAULT_SIGBUS
;
234 result
= dax_pmd_fault(vma
, addr
, pmd
, flags
,
239 ext4_journal_stop(handle
);
240 up_read(&EXT4_I(inode
)->i_mmap_sem
);
241 sb_end_pagefault(sb
);
243 up_read(&EXT4_I(inode
)->i_mmap_sem
);
249 * Handle write fault for VM_MIXEDMAP mappings. Similarly to ext4_dax_fault()
250 * handler we check for races agaist truncate. Note that since we cycle through
251 * i_mmap_sem, we are sure that also any hole punching that began before we
252 * were called is finished by now and so if it included part of the file we
253 * are working on, our pte will get unmapped and the check for pte_same() in
254 * wp_pfn_shared() fails. Thus fault gets retried and things work out as
257 static int ext4_dax_pfn_mkwrite(struct vm_area_struct
*vma
,
258 struct vm_fault
*vmf
)
260 struct inode
*inode
= file_inode(vma
->vm_file
);
261 struct super_block
*sb
= inode
->i_sb
;
265 sb_start_pagefault(sb
);
266 file_update_time(vma
->vm_file
);
267 down_read(&EXT4_I(inode
)->i_mmap_sem
);
268 size
= (i_size_read(inode
) + PAGE_SIZE
- 1) >> PAGE_SHIFT
;
269 if (vmf
->pgoff
>= size
)
270 ret
= VM_FAULT_SIGBUS
;
272 ret
= dax_pfn_mkwrite(vma
, vmf
);
273 up_read(&EXT4_I(inode
)->i_mmap_sem
);
274 sb_end_pagefault(sb
);
279 static const struct vm_operations_struct ext4_dax_vm_ops
= {
280 .fault
= ext4_dax_fault
,
281 .pmd_fault
= ext4_dax_pmd_fault
,
282 .page_mkwrite
= ext4_dax_fault
,
283 .pfn_mkwrite
= ext4_dax_pfn_mkwrite
,
286 #define ext4_dax_vm_ops ext4_file_vm_ops
289 static const struct vm_operations_struct ext4_file_vm_ops
= {
290 .fault
= ext4_filemap_fault
,
291 .map_pages
= filemap_map_pages
,
292 .page_mkwrite
= ext4_page_mkwrite
,
295 static int ext4_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
297 struct inode
*inode
= file
->f_mapping
->host
;
299 if (ext4_encrypted_inode(inode
)) {
300 int err
= fscrypt_get_encryption_info(inode
);
303 if (!fscrypt_has_encryption_key(inode
))
307 if (IS_DAX(file_inode(file
))) {
308 vma
->vm_ops
= &ext4_dax_vm_ops
;
309 vma
->vm_flags
|= VM_MIXEDMAP
| VM_HUGEPAGE
;
311 vma
->vm_ops
= &ext4_file_vm_ops
;
316 static int ext4_file_open(struct inode
* inode
, struct file
* filp
)
318 struct super_block
*sb
= inode
->i_sb
;
319 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
320 struct vfsmount
*mnt
= filp
->f_path
.mnt
;
326 if (unlikely(!(sbi
->s_mount_flags
& EXT4_MF_MNTDIR_SAMPLED
) &&
327 !(sb
->s_flags
& MS_RDONLY
))) {
328 sbi
->s_mount_flags
|= EXT4_MF_MNTDIR_SAMPLED
;
330 * Sample where the filesystem has been mounted and
331 * store it in the superblock for sysadmin convenience
332 * when trying to sort through large numbers of block
333 * devices or filesystem images.
335 memset(buf
, 0, sizeof(buf
));
337 path
.dentry
= mnt
->mnt_root
;
338 cp
= d_path(&path
, buf
, sizeof(buf
));
343 handle
= ext4_journal_start_sb(sb
, EXT4_HT_MISC
, 1);
345 return PTR_ERR(handle
);
346 BUFFER_TRACE(sbi
->s_sbh
, "get_write_access");
347 err
= ext4_journal_get_write_access(handle
, sbi
->s_sbh
);
349 ext4_journal_stop(handle
);
352 strlcpy(sbi
->s_es
->s_last_mounted
, cp
,
353 sizeof(sbi
->s_es
->s_last_mounted
));
354 ext4_handle_dirty_super(handle
, sb
);
355 ext4_journal_stop(handle
);
358 if (ext4_encrypted_inode(inode
)) {
359 ret
= fscrypt_get_encryption_info(inode
);
362 if (!fscrypt_has_encryption_key(inode
))
366 dir
= dget_parent(file_dentry(filp
));
367 if (ext4_encrypted_inode(d_inode(dir
)) &&
368 !fscrypt_has_permitted_context(d_inode(dir
), inode
)) {
369 ext4_warning(inode
->i_sb
,
370 "Inconsistent encryption contexts: %lu/%lu",
371 (unsigned long) d_inode(dir
)->i_ino
,
372 (unsigned long) inode
->i_ino
);
378 * Set up the jbd2_inode if we are opening the inode for
379 * writing and the journal is present
381 if (filp
->f_mode
& FMODE_WRITE
) {
382 ret
= ext4_inode_attach_jinode(inode
);
386 return dquot_file_open(inode
, filp
);
390 * Here we use ext4_map_blocks() to get a block mapping for a extent-based
391 * file rather than ext4_ext_walk_space() because we can introduce
392 * SEEK_DATA/SEEK_HOLE for block-mapped and extent-mapped file at the same
393 * function. When extent status tree has been fully implemented, it will
394 * track all extent status for a file and we can directly use it to
395 * retrieve the offset for SEEK_DATA/SEEK_HOLE.
399 * When we retrieve the offset for SEEK_DATA/SEEK_HOLE, we would need to
400 * lookup page cache to check whether or not there has some data between
401 * [startoff, endoff] because, if this range contains an unwritten extent,
402 * we determine this extent as a data or a hole according to whether the
403 * page cache has data or not.
405 static int ext4_find_unwritten_pgoff(struct inode
*inode
,
411 unsigned int blkbits
;
419 blkbits
= inode
->i_sb
->s_blocksize_bits
;
422 endoff
= (loff_t
)end_blk
<< blkbits
;
424 index
= startoff
>> PAGE_SHIFT
;
425 end
= endoff
>> PAGE_SHIFT
;
427 pagevec_init(&pvec
, 0);
430 unsigned long nr_pages
;
432 num
= min_t(pgoff_t
, end
- index
, PAGEVEC_SIZE
);
433 nr_pages
= pagevec_lookup(&pvec
, inode
->i_mapping
, index
,
436 if (whence
== SEEK_DATA
)
439 BUG_ON(whence
!= SEEK_HOLE
);
441 * If this is the first time to go into the loop and
442 * offset is not beyond the end offset, it will be a
443 * hole at this offset
445 if (lastoff
== startoff
|| lastoff
< endoff
)
451 * If this is the first time to go into the loop and
452 * offset is smaller than the first page offset, it will be a
453 * hole at this offset.
455 if (lastoff
== startoff
&& whence
== SEEK_HOLE
&&
456 lastoff
< page_offset(pvec
.pages
[0])) {
461 for (i
= 0; i
< nr_pages
; i
++) {
462 struct page
*page
= pvec
.pages
[i
];
463 struct buffer_head
*bh
, *head
;
466 * If the current offset is not beyond the end of given
467 * range, it will be a hole.
469 if (lastoff
< endoff
&& whence
== SEEK_HOLE
&&
478 if (unlikely(page
->mapping
!= inode
->i_mapping
)) {
483 if (!page_has_buffers(page
)) {
488 if (page_has_buffers(page
)) {
489 lastoff
= page_offset(page
);
490 bh
= head
= page_buffers(page
);
492 if (buffer_uptodate(bh
) ||
493 buffer_unwritten(bh
)) {
494 if (whence
== SEEK_DATA
)
497 if (whence
== SEEK_HOLE
)
501 *offset
= max_t(loff_t
,
506 lastoff
+= bh
->b_size
;
507 bh
= bh
->b_this_page
;
508 } while (bh
!= head
);
511 lastoff
= page_offset(page
) + PAGE_SIZE
;
516 * The no. of pages is less than our desired, that would be a
519 if (nr_pages
< num
&& whence
== SEEK_HOLE
) {
525 index
= pvec
.pages
[i
- 1]->index
+ 1;
526 pagevec_release(&pvec
);
527 } while (index
<= end
);
530 pagevec_release(&pvec
);
535 * ext4_seek_data() retrieves the offset for SEEK_DATA.
537 static loff_t
ext4_seek_data(struct file
*file
, loff_t offset
, loff_t maxsize
)
539 struct inode
*inode
= file
->f_mapping
->host
;
540 struct extent_status es
;
541 ext4_lblk_t start
, last
, end
;
542 loff_t dataoff
, isize
;
548 isize
= i_size_read(inode
);
549 if (offset
>= isize
) {
554 blkbits
= inode
->i_sb
->s_blocksize_bits
;
555 start
= offset
>> blkbits
;
557 end
= isize
>> blkbits
;
561 ret
= ext4_get_next_extent(inode
, last
, end
- last
+ 1, &es
);
563 /* No extent found -> no data */
572 dataoff
= (loff_t
)last
<< blkbits
;
573 if (!ext4_es_is_unwritten(&es
))
577 * If there is a unwritten extent at this offset,
578 * it will be as a data or a hole according to page
579 * cache that has data or not.
581 if (ext4_find_unwritten_pgoff(inode
, SEEK_DATA
,
582 es
.es_lblk
+ es
.es_len
, &dataoff
))
585 dataoff
= (loff_t
)last
<< blkbits
;
587 } while (last
<= end
);
594 return vfs_setpos(file
, dataoff
, maxsize
);
598 * ext4_seek_hole() retrieves the offset for SEEK_HOLE.
600 static loff_t
ext4_seek_hole(struct file
*file
, loff_t offset
, loff_t maxsize
)
602 struct inode
*inode
= file
->f_mapping
->host
;
603 struct extent_status es
;
604 ext4_lblk_t start
, last
, end
;
605 loff_t holeoff
, isize
;
611 isize
= i_size_read(inode
);
612 if (offset
>= isize
) {
617 blkbits
= inode
->i_sb
->s_blocksize_bits
;
618 start
= offset
>> blkbits
;
620 end
= isize
>> blkbits
;
624 ret
= ext4_get_next_extent(inode
, last
, end
- last
+ 1, &es
);
630 if (ret
== 0 || es
.es_lblk
> last
) {
632 holeoff
= (loff_t
)last
<< blkbits
;
636 * If there is a unwritten extent at this offset,
637 * it will be as a data or a hole according to page
638 * cache that has data or not.
640 if (ext4_es_is_unwritten(&es
) &&
641 ext4_find_unwritten_pgoff(inode
, SEEK_HOLE
,
642 last
+ es
.es_len
, &holeoff
))
646 holeoff
= (loff_t
)last
<< blkbits
;
648 } while (last
<= end
);
655 return vfs_setpos(file
, holeoff
, maxsize
);
659 * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
660 * by calling generic_file_llseek_size() with the appropriate maxbytes
663 loff_t
ext4_llseek(struct file
*file
, loff_t offset
, int whence
)
665 struct inode
*inode
= file
->f_mapping
->host
;
668 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
669 maxbytes
= EXT4_SB(inode
->i_sb
)->s_bitmap_maxbytes
;
671 maxbytes
= inode
->i_sb
->s_maxbytes
;
677 return generic_file_llseek_size(file
, offset
, whence
,
678 maxbytes
, i_size_read(inode
));
680 return ext4_seek_data(file
, offset
, maxbytes
);
682 return ext4_seek_hole(file
, offset
, maxbytes
);
688 const struct file_operations ext4_file_operations
= {
689 .llseek
= ext4_llseek
,
690 .read_iter
= generic_file_read_iter
,
691 .write_iter
= ext4_file_write_iter
,
692 .unlocked_ioctl
= ext4_ioctl
,
694 .compat_ioctl
= ext4_compat_ioctl
,
696 .mmap
= ext4_file_mmap
,
697 .open
= ext4_file_open
,
698 .release
= ext4_release_file
,
699 .fsync
= ext4_sync_file
,
700 .get_unmapped_area
= thp_get_unmapped_area
,
701 .splice_read
= generic_file_splice_read
,
702 .splice_write
= iter_file_splice_write
,
703 .fallocate
= ext4_fallocate
,
706 const struct inode_operations ext4_file_inode_operations
= {
707 .setattr
= ext4_setattr
,
708 .getattr
= ext4_getattr
,
709 .listxattr
= ext4_listxattr
,
710 .get_acl
= ext4_get_acl
,
711 .set_acl
= ext4_set_acl
,
712 .fiemap
= ext4_fiemap
,