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 >= ALIGN(i_size_read(inode), sb->s_blocksize))
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);
167 * Unaligned direct AIO must be the only IO in flight. Otherwise
168 * overlapping aligned IO after unaligned might result in data
171 if (ret == -EIOCBQUEUED && unaligned_aio)
172 ext4_unwritten_wait(inode);
176 ret = generic_write_sync(iocb, ret);
186 static int ext4_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
189 handle_t *handle = NULL;
190 struct inode *inode = file_inode(vma->vm_file);
191 struct super_block *sb = inode->i_sb;
192 bool write = vmf->flags & FAULT_FLAG_WRITE;
195 sb_start_pagefault(sb);
196 file_update_time(vma->vm_file);
197 down_read(&EXT4_I(inode)->i_mmap_sem);
198 handle = ext4_journal_start_sb(sb, EXT4_HT_WRITE_PAGE,
199 EXT4_DATA_TRANS_BLOCKS(sb));
201 down_read(&EXT4_I(inode)->i_mmap_sem);
204 result = VM_FAULT_SIGBUS;
206 result = dax_fault(vma, vmf, ext4_dax_get_block);
210 ext4_journal_stop(handle);
211 up_read(&EXT4_I(inode)->i_mmap_sem);
212 sb_end_pagefault(sb);
214 up_read(&EXT4_I(inode)->i_mmap_sem);
219 static int ext4_dax_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
220 pmd_t *pmd, unsigned int flags)
223 handle_t *handle = NULL;
224 struct inode *inode = file_inode(vma->vm_file);
225 struct super_block *sb = inode->i_sb;
226 bool write = flags & FAULT_FLAG_WRITE;
229 sb_start_pagefault(sb);
230 file_update_time(vma->vm_file);
231 down_read(&EXT4_I(inode)->i_mmap_sem);
232 handle = ext4_journal_start_sb(sb, EXT4_HT_WRITE_PAGE,
233 ext4_chunk_trans_blocks(inode,
234 PMD_SIZE / PAGE_SIZE));
236 down_read(&EXT4_I(inode)->i_mmap_sem);
239 result = VM_FAULT_SIGBUS;
241 result = dax_pmd_fault(vma, addr, pmd, flags,
246 ext4_journal_stop(handle);
247 up_read(&EXT4_I(inode)->i_mmap_sem);
248 sb_end_pagefault(sb);
250 up_read(&EXT4_I(inode)->i_mmap_sem);
256 * Handle write fault for VM_MIXEDMAP mappings. Similarly to ext4_dax_fault()
257 * handler we check for races agaist truncate. Note that since we cycle through
258 * i_mmap_sem, we are sure that also any hole punching that began before we
259 * were called is finished by now and so if it included part of the file we
260 * are working on, our pte will get unmapped and the check for pte_same() in
261 * wp_pfn_shared() fails. Thus fault gets retried and things work out as
264 static int ext4_dax_pfn_mkwrite(struct vm_area_struct *vma,
265 struct vm_fault *vmf)
267 struct inode *inode = file_inode(vma->vm_file);
268 struct super_block *sb = inode->i_sb;
272 sb_start_pagefault(sb);
273 file_update_time(vma->vm_file);
274 down_read(&EXT4_I(inode)->i_mmap_sem);
275 size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
276 if (vmf->pgoff >= size)
277 ret = VM_FAULT_SIGBUS;
279 ret = dax_pfn_mkwrite(vma, vmf);
280 up_read(&EXT4_I(inode)->i_mmap_sem);
281 sb_end_pagefault(sb);
286 static const struct vm_operations_struct ext4_dax_vm_ops = {
287 .fault = ext4_dax_fault,
288 .pmd_fault = ext4_dax_pmd_fault,
289 .page_mkwrite = ext4_dax_fault,
290 .pfn_mkwrite = ext4_dax_pfn_mkwrite,
293 #define ext4_dax_vm_ops ext4_file_vm_ops
296 static const struct vm_operations_struct ext4_file_vm_ops = {
297 .fault = ext4_filemap_fault,
298 .map_pages = filemap_map_pages,
299 .page_mkwrite = ext4_page_mkwrite,
302 static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
304 struct inode *inode = file->f_mapping->host;
306 if (ext4_encrypted_inode(inode)) {
307 int err = fscrypt_get_encryption_info(inode);
310 if (!fscrypt_has_encryption_key(inode))
314 if (IS_DAX(file_inode(file))) {
315 vma->vm_ops = &ext4_dax_vm_ops;
316 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
318 vma->vm_ops = &ext4_file_vm_ops;
323 static int ext4_file_open(struct inode * inode, struct file * filp)
325 struct super_block *sb = inode->i_sb;
326 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
327 struct vfsmount *mnt = filp->f_path.mnt;
333 if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
334 !(sb->s_flags & MS_RDONLY))) {
335 sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
337 * Sample where the filesystem has been mounted and
338 * store it in the superblock for sysadmin convenience
339 * when trying to sort through large numbers of block
340 * devices or filesystem images.
342 memset(buf, 0, sizeof(buf));
344 path.dentry = mnt->mnt_root;
345 cp = d_path(&path, buf, sizeof(buf));
350 handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
352 return PTR_ERR(handle);
353 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
354 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
356 ext4_journal_stop(handle);
359 strlcpy(sbi->s_es->s_last_mounted, cp,
360 sizeof(sbi->s_es->s_last_mounted));
361 ext4_handle_dirty_super(handle, sb);
362 ext4_journal_stop(handle);
365 if (ext4_encrypted_inode(inode)) {
366 ret = fscrypt_get_encryption_info(inode);
369 if (!fscrypt_has_encryption_key(inode))
373 dir = dget_parent(file_dentry(filp));
374 if (ext4_encrypted_inode(d_inode(dir)) &&
375 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
376 ext4_warning(inode->i_sb,
377 "Inconsistent encryption contexts: %lu/%lu",
378 (unsigned long) d_inode(dir)->i_ino,
379 (unsigned long) inode->i_ino);
385 * Set up the jbd2_inode if we are opening the inode for
386 * writing and the journal is present
388 if (filp->f_mode & FMODE_WRITE) {
389 ret = ext4_inode_attach_jinode(inode);
393 return dquot_file_open(inode, filp);
397 * Here we use ext4_map_blocks() to get a block mapping for a extent-based
398 * file rather than ext4_ext_walk_space() because we can introduce
399 * SEEK_DATA/SEEK_HOLE for block-mapped and extent-mapped file at the same
400 * function. When extent status tree has been fully implemented, it will
401 * track all extent status for a file and we can directly use it to
402 * retrieve the offset for SEEK_DATA/SEEK_HOLE.
406 * When we retrieve the offset for SEEK_DATA/SEEK_HOLE, we would need to
407 * lookup page cache to check whether or not there has some data between
408 * [startoff, endoff] because, if this range contains an unwritten extent,
409 * we determine this extent as a data or a hole according to whether the
410 * page cache has data or not.
412 static int ext4_find_unwritten_pgoff(struct inode *inode,
418 unsigned int blkbits;
426 blkbits = inode->i_sb->s_blocksize_bits;
429 endoff = (loff_t)end_blk << blkbits;
431 index = startoff >> PAGE_SHIFT;
432 end = endoff >> PAGE_SHIFT;
434 pagevec_init(&pvec, 0);
437 unsigned long nr_pages;
439 num = min_t(pgoff_t, end - index, PAGEVEC_SIZE - 1) + 1;
440 nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index,
445 for (i = 0; i < nr_pages; i++) {
446 struct page *page = pvec.pages[i];
447 struct buffer_head *bh, *head;
450 * If current offset is smaller than the page offset,
451 * there is a hole at this offset.
453 if (whence == SEEK_HOLE && lastoff < endoff &&
454 lastoff < page_offset(pvec.pages[i])) {
460 if (page->index > end)
465 if (unlikely(page->mapping != inode->i_mapping)) {
470 if (!page_has_buffers(page)) {
475 if (page_has_buffers(page)) {
476 lastoff = page_offset(page);
477 bh = head = page_buffers(page);
479 if (lastoff + bh->b_size <= startoff)
481 if (buffer_uptodate(bh) ||
482 buffer_unwritten(bh)) {
483 if (whence == SEEK_DATA)
486 if (whence == SEEK_HOLE)
490 *offset = max_t(loff_t,
496 lastoff += bh->b_size;
497 bh = bh->b_this_page;
498 } while (bh != head);
501 lastoff = page_offset(page) + PAGE_SIZE;
505 /* The no. of pages is less than our desired, we are done. */
509 index = pvec.pages[i - 1]->index + 1;
510 pagevec_release(&pvec);
511 } while (index <= end);
513 if (whence == SEEK_HOLE && lastoff < endoff) {
518 pagevec_release(&pvec);
523 * ext4_seek_data() retrieves the offset for SEEK_DATA.
525 static loff_t ext4_seek_data(struct file *file, loff_t offset, loff_t maxsize)
527 struct inode *inode = file->f_mapping->host;
528 struct extent_status es;
529 ext4_lblk_t start, last, end;
530 loff_t dataoff, isize;
536 isize = i_size_read(inode);
537 if (offset < 0 || offset >= isize) {
542 blkbits = inode->i_sb->s_blocksize_bits;
543 start = offset >> blkbits;
545 end = isize >> blkbits;
549 ret = ext4_get_next_extent(inode, last, end - last + 1, &es);
551 /* No extent found -> no data */
560 dataoff = (loff_t)last << blkbits;
561 if (!ext4_es_is_unwritten(&es))
565 * If there is a unwritten extent at this offset,
566 * it will be as a data or a hole according to page
567 * cache that has data or not.
569 if (ext4_find_unwritten_pgoff(inode, SEEK_DATA,
570 es.es_lblk + es.es_len, &dataoff))
573 dataoff = (loff_t)last << blkbits;
575 } while (last <= end);
582 return vfs_setpos(file, dataoff, maxsize);
586 * ext4_seek_hole() retrieves the offset for SEEK_HOLE.
588 static loff_t ext4_seek_hole(struct file *file, loff_t offset, loff_t maxsize)
590 struct inode *inode = file->f_mapping->host;
591 struct extent_status es;
592 ext4_lblk_t start, last, end;
593 loff_t holeoff, isize;
599 isize = i_size_read(inode);
600 if (offset < 0 || offset >= isize) {
605 blkbits = inode->i_sb->s_blocksize_bits;
606 start = offset >> blkbits;
608 end = isize >> blkbits;
612 ret = ext4_get_next_extent(inode, last, end - last + 1, &es);
618 if (ret == 0 || es.es_lblk > last) {
620 holeoff = (loff_t)last << blkbits;
624 * If there is a unwritten extent at this offset,
625 * it will be as a data or a hole according to page
626 * cache that has data or not.
628 if (ext4_es_is_unwritten(&es) &&
629 ext4_find_unwritten_pgoff(inode, SEEK_HOLE,
630 last + es.es_len, &holeoff))
634 holeoff = (loff_t)last << blkbits;
636 } while (last <= end);
643 return vfs_setpos(file, holeoff, maxsize);
647 * ext4_llseek() handles both block-mapped and extent-mapped maxbytes values
648 * by calling generic_file_llseek_size() with the appropriate maxbytes
651 loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
653 struct inode *inode = file->f_mapping->host;
656 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
657 maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
659 maxbytes = inode->i_sb->s_maxbytes;
665 return generic_file_llseek_size(file, offset, whence,
666 maxbytes, i_size_read(inode));
668 return ext4_seek_data(file, offset, maxbytes);
670 return ext4_seek_hole(file, offset, maxbytes);
676 const struct file_operations ext4_file_operations = {
677 .llseek = ext4_llseek,
678 .read_iter = generic_file_read_iter,
679 .write_iter = ext4_file_write_iter,
680 .unlocked_ioctl = ext4_ioctl,
682 .compat_ioctl = ext4_compat_ioctl,
684 .mmap = ext4_file_mmap,
685 .open = ext4_file_open,
686 .release = ext4_release_file,
687 .fsync = ext4_sync_file,
688 .get_unmapped_area = thp_get_unmapped_area,
689 .splice_read = generic_file_splice_read,
690 .splice_write = iter_file_splice_write,
691 .fallocate = ext4_fallocate,
694 const struct inode_operations ext4_file_inode_operations = {
695 .setattr = ext4_setattr,
696 .getattr = ext4_getattr,
697 .listxattr = ext4_listxattr,
698 .get_acl = ext4_get_acl,
699 .set_acl = ext4_set_acl,
700 .fiemap = ext4_fiemap,