Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / fs / ext4 / file.c
1 /*
2  *  linux/fs/ext4/file.c
3  *
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)
8  *
9  *  from
10  *
11  *  linux/fs/minix/file.c
12  *
13  *  Copyright (C) 1991, 1992  Linus Torvalds
14  *
15  *  ext4 fs regular file handling primitives
16  *
17  *  64-bit file support on 64-bit platforms by Jakub Jelinek
18  *      (jj@sunsite.ms.mff.cuni.cz)
19  */
20
21 #include <linux/time.h>
22 #include <linux/fs.h>
23 #include <linux/jbd2.h>
24 #include <linux/mount.h>
25 #include <linux/path.h>
26 #include <linux/quotaops.h>
27 #include "ext4.h"
28 #include "ext4_jbd2.h"
29 #include "xattr.h"
30 #include "acl.h"
31 #include "richacl.h"
32
33 /*
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.
37  */
38 static int ext4_release_file(struct inode *inode, struct file *filp)
39 {
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);
43         }
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)
48         {
49                 down_write(&EXT4_I(inode)->i_data_sem);
50                 ext4_discard_preallocations(inode);
51                 up_write(&EXT4_I(inode)->i_data_sem);
52         }
53         if (is_dx(inode) && filp->private_data)
54                 ext4_htree_free_dir_info(filp->private_data);
55
56         return 0;
57 }
58
59 static void ext4_aiodio_wait(struct inode *inode)
60 {
61         wait_queue_head_t *wq = ext4_ioend_wq(inode);
62
63         wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_aiodio_unwritten) == 0));
64 }
65
66 /*
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.
74  */
75 static int
76 ext4_unaligned_aio(struct inode *inode, const struct iovec *iov,
77                    unsigned long nr_segs, loff_t pos)
78 {
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;
83
84         if (pos >= inode->i_size)
85                 return 0;
86
87         if ((pos & blockmask) || (final_size & blockmask))
88                 return 1;
89
90         return 0;
91 }
92
93 static ssize_t
94 ext4_file_write(struct kiocb *iocb, const struct iovec *iov,
95                 unsigned long nr_segs, loff_t pos)
96 {
97         struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
98         int unaligned_aio = 0;
99         int ret;
100
101         /*
102          * If we have encountered a bitmap-format file, the size limit
103          * is smaller than s_maxbytes, which is for extent-mapped files.
104          */
105
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);
109
110                 if ((pos > sbi->s_bitmap_maxbytes ||
111                     (pos == sbi->s_bitmap_maxbytes && length > 0)))
112                         return -EFBIG;
113
114                 if (pos + length > sbi->s_bitmap_maxbytes) {
115                         nr_segs = iov_shorten((struct iovec *)iov, nr_segs,
116                                               sbi->s_bitmap_maxbytes - pos);
117                 }
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);
121         }
122
123         /* Unaligned direct AIO must be serialized; see comment above */
124         if (unaligned_aio) {
125                 static unsigned long unaligned_warn_time;
126
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);
135         }
136
137         ret = generic_file_aio_write(iocb, iov, nr_segs, pos);
138
139         if (unaligned_aio)
140                 mutex_unlock(ext4_aio_mutex(inode));
141
142         return ret;
143 }
144
145 static const struct vm_operations_struct ext4_file_vm_ops = {
146         .fault          = filemap_fault,
147         .page_mkwrite   = ext4_page_mkwrite,
148 };
149
150 static int ext4_file_mmap(struct file *file, struct vm_area_struct *vma)
151 {
152         struct address_space *mapping = file->f_mapping;
153
154         if (!mapping->a_ops->readpage)
155                 return -ENOEXEC;
156         file_accessed(file);
157         vma->vm_ops = &ext4_file_vm_ops;
158         vma->vm_flags |= VM_CAN_NONLINEAR;
159         return 0;
160 }
161
162 static int ext4_file_open(struct inode * inode, struct file * filp)
163 {
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;
168         struct path path;
169         char buf[64], *cp;
170
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;
174                 /*
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.
179                  */
180                 memset(buf, 0, sizeof(buf));
181                 path.mnt = mnt;
182                 path.dentry = mnt->mnt_root;
183                 cp = d_path(&path, buf, sizeof(buf));
184                 if (!IS_ERR(cp)) {
185                         strlcpy(sbi->s_es->s_last_mounted, cp,
186                                 sizeof(sbi->s_es->s_last_mounted));
187                         ext4_mark_super_dirty(sb);
188                 }
189         }
190         /*
191          * Set up the jbd2_inode if we are opening the inode for
192          * writing and the journal is present
193          */
194         if (sbi->s_journal && !ei->jinode && (filp->f_mode & FMODE_WRITE)) {
195                 struct jbd2_inode *jinode = jbd2_alloc_inode(GFP_KERNEL);
196
197                 spin_lock(&inode->i_lock);
198                 if (!ei->jinode) {
199                         if (!jinode) {
200                                 spin_unlock(&inode->i_lock);
201                                 return -ENOMEM;
202                         }
203                         ei->jinode = jinode;
204                         jbd2_journal_init_jbd_inode(ei->jinode, inode);
205                         jinode = NULL;
206                 }
207                 spin_unlock(&inode->i_lock);
208                 if (unlikely(jinode != NULL))
209                         jbd2_free_inode(jinode);
210         }
211         return dquot_file_open(inode, filp);
212 }
213
214 /*
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().
218  */
219 loff_t ext4_llseek(struct file *file, loff_t offset, int origin)
220 {
221         struct inode *inode = file->f_mapping->host;
222         loff_t maxbytes;
223
224         if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
225                 maxbytes = EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
226         else
227                 maxbytes = inode->i_sb->s_maxbytes;
228
229         return generic_file_llseek_size(file, offset, origin, maxbytes);
230 }
231
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,
239 #ifdef CONFIG_COMPAT
240         .compat_ioctl   = ext4_compat_ioctl,
241 #endif
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,
249 };
250
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,
259 #endif
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,
265 };
266