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/jbd2.h>
24 #include <linux/mount.h>
25 #include <linux/path.h>
26 #include <linux/quotaops.h>
28 #include "ext4_jbd2.h"
34 * Called when an inode is released. Note that this is different
35 * from ext4_file_open: open gets called at every open, but release
36 * gets called only when /all/ the files are closed.
38 static int ext4_release_file(struct inode *inode, struct file *filp)
40 if (ext4_test_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE)) {
41 ext4_alloc_da_blocks(inode);
42 ext4_clear_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
44 /* if we are the last writer on the inode, drop the block reservation */
45 if ((filp->f_mode & FMODE_WRITE) &&
46 (atomic_read(&inode->i_writecount) == 1) &&
47 !EXT4_I(inode)->i_reserved_data_blocks)
49 down_write(&EXT4_I(inode)->i_data_sem);
50 ext4_discard_preallocations(inode);
51 up_write(&EXT4_I(inode)->i_data_sem);
53 if (is_dx(inode) && filp->private_data)
54 ext4_htree_free_dir_info(filp->private_data);
59 static void ext4_aiodio_wait(struct inode *inode)
61 wait_queue_head_t *wq = ext4_ioend_wq(inode);
63 wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_aiodio_unwritten) == 0));
67 * This tests whether the IO in question is block-aligned or not.
68 * Ext4 utilizes unwritten extents when hole-filling during direct IO, and they
69 * are converted to written only after the IO is complete. Until they are
70 * mapped, these blocks appear as holes, so dio_zero_block() will assume that
71 * it needs to zero out portions of the start and/or end block. If 2 AIO
72 * threads are at work on the same unwritten block, they must be synchronized
73 * or one thread will zero the other's data, causing corruption.
76 ext4_unaligned_aio(struct inode *inode, const struct iovec *iov,
77 unsigned long nr_segs, loff_t pos)
79 struct super_block *sb = inode->i_sb;
80 int blockmask = sb->s_blocksize - 1;
81 size_t count = iov_length(iov, nr_segs);
82 loff_t final_size = pos + count;
84 if (pos >= inode->i_size)
87 if ((pos & blockmask) || (final_size & blockmask))
94 ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
95 unsigned long nr_segs, loff_t pos)
97 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
98 int unaligned_aio = 0;
102 * If we have encountered a bitmap-format file, the size limit
103 * is smaller than s_maxbytes, which is for extent-mapped files.
106 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
107 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
108 size_t length = iov_length(iov, nr_segs);
110 if ((pos > sbi->s_bitmap_maxbytes ||
111 (pos == sbi->s_bitmap_maxbytes && length > 0)))
114 if (pos + length > sbi->s_bitmap_maxbytes) {
115 nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
116 sbi->s_bitmap_maxbytes - pos);
118 } else if (unlikely((iocb->ki_filp->f_flags & O_DIRECT) &&
119 !is_sync_kiocb(iocb))) {
120 unaligned_aio = ext4_unaligned_aio(inode, iov, nr_segs, pos);
123 /* Unaligned direct AIO must be serialized; see comment above */
125 static unsigned long unaligned_warn_time;
127 /* Warn about this once per day */
128 if (printk_timed_ratelimit(&unaligned_warn_time, 60*60*24*HZ))
129 ext4_msg(inode->i_sb, KERN_WARNING,
130 "Unaligned AIO/DIO on inode %ld by %s; "
131 "performance will be poor.",
132 inode->i_ino, current->comm);
133 mutex_lock(ext4_aio_mutex(inode));
134 ext4_aiodio_wait(inode);
137 ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
140 mutex_unlock(ext4_aio_mutex(inode));
145 static const struct vm_operations_struct ext4_file_vm_ops = {
146 .fault = filemap_fault,
147 .page_mkwrite = ext4_page_mkwrite,
150 static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
152 struct address_space *mapping = file->f_mapping;
154 if (!mapping->a_ops->readpage)
157 vma->vm_ops = &ext4_file_vm_ops;
158 vma->vm_flags |= VM_CAN_NONLINEAR;
162 static int ext4_file_open(struct inode * inode, struct file * filp)
164 struct super_block *sb = inode->i_sb;
165 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
166 struct ext4_inode_info *ei = EXT4_I(inode);
167 struct vfsmount *mnt = filp->f_path.mnt;
171 if (unlikely(!(sbi->s_mount_flags & EXT4_MF_MNTDIR_SAMPLED) &&
172 !(sb->s_flags & MS_RDONLY))) {
173 sbi->s_mount_flags |= EXT4_MF_MNTDIR_SAMPLED;
175 * Sample where the filesystem has been mounted and
176 * store it in the superblock for sysadmin convenience
177 * when trying to sort through large numbers of block
178 * devices or filesystem images.
180 memset(buf, 0, sizeof(buf));
182 path.dentry = mnt->mnt_root;
183 cp = d_path(&path, buf, sizeof(buf));
185 strlcpy(sbi->s_es->s_last_mounted, cp,
186 sizeof(sbi->s_es->s_last_mounted));
187 ext4_mark_super_dirty(sb);
191 * Set up the jbd2_inode if we are opening the inode for
192 * writing and the journal is present
194 if (sbi->s_journal && !ei->jinode && (filp->f_mode & FMODE_WRITE)) {
195 struct jbd2_inode *jinode = jbd2_alloc_inode(GFP_KERNEL);
197 spin_lock(&inode->i_lock);
200 spin_unlock(&inode->i_lock);
204 jbd2_journal_init_jbd_inode(ei->jinode, inode);
207 spin_unlock(&inode->i_lock);
208 if (unlikely(jinode != NULL))
209 jbd2_free_inode(jinode);
211 return dquot_file_open(inode, filp);
215 * ext4_llseek() copied from generic_file_llseek() to handle both
216 * block-mapped and extent-mapped maxbytes values. This should
217 * otherwise be identical with generic_file_llseek().
219 loff_t ext4_llseek(struct file *file, loff_t offset, int origin)
221 struct inode *inode = file->f_mapping->host;
224 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
225 maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
227 maxbytes = inode->i_sb->s_maxbytes;
229 return generic_file_llseek_size(file, offset, origin, maxbytes);
232 const struct file_operations ext4_file_operations = {
233 .llseek = ext4_llseek,
234 .read = do_sync_read,
235 .write = do_sync_write,
236 .aio_read = generic_file_aio_read,
237 .aio_write = ext4_file_write,
238 .unlocked_ioctl = ext4_ioctl,
240 .compat_ioctl = ext4_compat_ioctl,
242 .mmap = ext4_file_mmap,
243 .open = ext4_file_open,
244 .release = ext4_release_file,
245 .fsync = ext4_sync_file,
246 .splice_read = generic_file_splice_read,
247 .splice_write = generic_file_splice_write,
248 .fallocate = ext4_fallocate,
251 const struct inode_operations ext4_file_inode_operations = {
252 .setattr = ext4_setattr,
253 .getattr = ext4_getattr,
254 #ifdef CONFIG_EXT4_FS_XATTR
255 .setxattr = generic_setxattr,
256 .getxattr = generic_getxattr,
257 .listxattr = ext4_listxattr,
258 .removexattr = generic_removexattr,
260 .get_acl = ext4_get_acl,
261 .fiemap = ext4_fiemap,
262 .permission = ext4_permission,
263 .may_create = ext4_may_create,
264 .may_delete = ext4_may_delete,